├── .gitignore ├── .prettierrc.json ├── README.md ├── TODOs.txt ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── manifest.json └── robots.txt └── src ├── App.css ├── App.js ├── Board ├── Board.css └── Board.jsx ├── index.css ├── index.js └── lib └── utils.js /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "printWidth": 80, 3 | "tabWidth": 2, 4 | "useTabs": false, 5 | "semi": true, 6 | "singleQuote": true, 7 | "jsxSingleQuote": false, 8 | "trailingComma": "all", 9 | "bracketSpacing": false, 10 | "jsxBracketSameLine": true, 11 | "arrowParens": "avoid" 12 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This project is meant to be a tutorial for a Snake game that reverses a linked list. 2 | 3 | Everything related to the tutorial (i.e., all the code that I, Clement, wrote) is located under /src/Board and /src/lib. The Board component is imported and rendered in App.js. 4 | 5 | If you want to work off of this base to create your own Snake game, feel free to fork this project or to just copy-paste code. Also, subscribe to my [YouTube channel](https://www.youtube.com/channel/UCaO6VoaYJv4kS-TQO_M-N_g) if you haven't already, and smash the like button on all my videos. Oh, and check out [AlgoExpert](https://www.algoexpert.io/product) if you're preparing for coding interviews. 6 | 7 | Everything below this line was automatically generated by Create React App. 8 | 9 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 10 | 11 | ## Available Scripts 12 | 13 | In the project directory, you can run: 14 | 15 | ### `npm start` 16 | 17 | Runs the app in the development mode.\ 18 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 19 | 20 | The page will reload if you make edits.\ 21 | You will also see any lint errors in the console. 22 | 23 | ### `npm test` 24 | 25 | Launches the test runner in the interactive watch mode.\ 26 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 27 | 28 | ### `npm run build` 29 | 30 | Builds the app for production to the `build` folder.\ 31 | It correctly bundles React in production mode and optimizes the build for the best performance. 32 | 33 | The build is minified and the filenames include the hashes.\ 34 | Your app is ready to be deployed! 35 | 36 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 37 | 38 | ### `npm run eject` 39 | 40 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 41 | 42 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 43 | 44 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 45 | 46 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 47 | 48 | ## Learn More 49 | 50 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 51 | 52 | To learn React, check out the [React documentation](https://reactjs.org/). 53 | 54 | ### Code Splitting 55 | 56 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) 57 | 58 | ### Analyzing the Bundle Size 59 | 60 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) 61 | 62 | ### Making a Progressive Web App 63 | 64 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) 65 | 66 | ### Advanced Configuration 67 | 68 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) 69 | 70 | ### Deployment 71 | 72 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) 73 | 74 | ### `npm run build` fails to minify 75 | 76 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) 77 | -------------------------------------------------------------------------------- /TODOs.txt: -------------------------------------------------------------------------------- 1 | Snake Game TODOs 2 | 3 | - [x] Create board 4 | - [x] Matrix representation 5 | - [x] Create snake 6 | - [x] Singly linked list representation 7 | - [x] Handle snake growth 8 | - [x] Handle snake direction reversal 9 | - [x] Styles 10 | - [x] Snake cells 11 | - [x] Food cells 12 | - [x] Handle keypresses 13 | - [x] Handle movements 14 | - [x] Handle food consumption 15 | - [x] Handle death 16 | - [x] Handle score 17 | - [ ] Handle game start and game reset <-------- TODO! -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "snake-game", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.11.9", 7 | "@testing-library/react": "^11.2.5", 8 | "@testing-library/user-event": "^12.8.3", 9 | "react": "^17.0.1", 10 | "react-dom": "^17.0.1", 11 | "react-scripts": "4.0.3", 12 | "web-vitals": "^1.1.1" 13 | }, 14 | "scripts": { 15 | "start": "react-scripts start", 16 | "build": "react-scripts build", 17 | "test": "react-scripts test", 18 | "eject": "react-scripts eject" 19 | }, 20 | "eslintConfig": { 21 | "extends": [ 22 | "react-app", 23 | "react-app/jest" 24 | ] 25 | }, 26 | "browserslist": { 27 | "production": [ 28 | ">0.2%", 29 | "not dead", 30 | "not op_mini all" 31 | ], 32 | "development": [ 33 | "last 1 chrome version", 34 | "last 1 firefox version", 35 | "last 1 safari version" 36 | ] 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clementmihailescu/Snake-Game-Reverse-LL-Tutorial/1c69a9a9dc28171a1445ef36649bfc2f7e66219e/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | background-color: #282c34; 4 | min-height: 100vh; 5 | display: flex; 6 | flex-direction: column; 7 | align-items: center; 8 | justify-content: center; 9 | font-size: calc(10px + 2vmin); 10 | color: white; 11 | } -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import Board from './Board/Board.jsx'; 2 | 3 | import './App.css'; 4 | 5 | const App = () => ( 6 |
7 | 8 |
9 | ); 10 | 11 | export default App; 12 | -------------------------------------------------------------------------------- /src/Board/Board.css: -------------------------------------------------------------------------------- 1 | .board { 2 | outline: 2px solid rgb(134, 154, 189); 3 | } 4 | 5 | .row { 6 | height: 35px; 7 | } 8 | 9 | .cell { 10 | width: 35px; 11 | height: 35px; 12 | outline: 1px solid rgb(134, 154, 189); 13 | display: inline-block; 14 | } 15 | 16 | .cell-green { 17 | background-color: green; 18 | } 19 | 20 | .cell-red { 21 | background-color: red; 22 | } 23 | 24 | .cell-purple { 25 | background-color: purple; 26 | } 27 | -------------------------------------------------------------------------------- /src/Board/Board.jsx: -------------------------------------------------------------------------------- 1 | import React, {useEffect, useState} from 'react'; 2 | import { 3 | randomIntFromInterval, 4 | reverseLinkedList, 5 | useInterval, 6 | } from '../lib/utils.js'; 7 | 8 | import './Board.css'; 9 | 10 | /** 11 | * TODO: add a more elegant UX for before a game starts and after a game ends. 12 | * A game probably shouldn't start until the user presses an arrow key, and 13 | * once a game is over, the board state should likely freeze until the user 14 | * intentionally restarts the game. 15 | */ 16 | 17 | class LinkedListNode { 18 | constructor(value) { 19 | this.value = value; 20 | this.next = null; 21 | } 22 | } 23 | 24 | class LinkedList { 25 | constructor(value) { 26 | const node = new LinkedListNode(value); 27 | this.head = node; 28 | this.tail = node; 29 | } 30 | } 31 | 32 | const Direction = { 33 | UP: 'UP', 34 | RIGHT: 'RIGHT', 35 | DOWN: 'DOWN', 36 | LEFT: 'LEFT', 37 | }; 38 | 39 | const BOARD_SIZE = 15; 40 | const PROBABILITY_OF_DIRECTION_REVERSAL_FOOD = 0.3; 41 | 42 | const getStartingSnakeLLValue = board => { 43 | const rowSize = board.length; 44 | const colSize = board[0].length; 45 | const startingRow = Math.round(rowSize / 3); 46 | const startingCol = Math.round(colSize / 3); 47 | const startingCell = board[startingRow][startingCol]; 48 | return { 49 | row: startingRow, 50 | col: startingCol, 51 | cell: startingCell, 52 | }; 53 | }; 54 | 55 | const Board = () => { 56 | const [score, setScore] = useState(0); 57 | const [board, setBoard] = useState(createBoard(BOARD_SIZE)); 58 | const [snake, setSnake] = useState( 59 | new LinkedList(getStartingSnakeLLValue(board)), 60 | ); 61 | const [snakeCells, setSnakeCells] = useState( 62 | new Set([snake.head.value.cell]), 63 | ); 64 | // Naively set the starting food cell 5 cells away from the starting snake cell. 65 | const [foodCell, setFoodCell] = useState(snake.head.value.cell + 5); 66 | const [direction, setDirection] = useState(Direction.RIGHT); 67 | const [foodShouldReverseDirection, setFoodShouldReverseDirection] = useState( 68 | false, 69 | ); 70 | 71 | useEffect(() => { 72 | window.addEventListener('keydown', e => { 73 | handleKeydown(e); 74 | }); 75 | }, []); 76 | 77 | // `useInterval` is needed; you can't naively do `setInterval` in the 78 | // `useEffect` above. See the article linked above the `useInterval` 79 | // definition for details. 80 | useInterval(() => { 81 | moveSnake(); 82 | }, 150); 83 | 84 | const handleKeydown = e => { 85 | const newDirection = getDirectionFromKey(e.key); 86 | const isValidDirection = newDirection !== ''; 87 | if (!isValidDirection) return; 88 | const snakeWillRunIntoItself = 89 | getOppositeDirection(newDirection) === direction && snakeCells.size > 1; 90 | // Note: this functionality is currently broken, for the same reason that 91 | // `useInterval` is needed. Specifically, the `direction` and `snakeCells` 92 | // will currently never reflect their "latest version" when `handleKeydown` 93 | // is called. I leave it as an exercise to the viewer to fix this :P 94 | if (snakeWillRunIntoItself) return; 95 | setDirection(newDirection); 96 | }; 97 | 98 | const moveSnake = () => { 99 | const currentHeadCoords = { 100 | row: snake.head.value.row, 101 | col: snake.head.value.col, 102 | }; 103 | 104 | const nextHeadCoords = getCoordsInDirection(currentHeadCoords, direction); 105 | if (isOutOfBounds(nextHeadCoords, board)) { 106 | handleGameOver(); 107 | return; 108 | } 109 | const nextHeadCell = board[nextHeadCoords.row][nextHeadCoords.col]; 110 | if (snakeCells.has(nextHeadCell)) { 111 | handleGameOver(); 112 | return; 113 | } 114 | 115 | const newHead = new LinkedListNode({ 116 | row: nextHeadCoords.row, 117 | col: nextHeadCoords.col, 118 | cell: nextHeadCell, 119 | }); 120 | const currentHead = snake.head; 121 | snake.head = newHead; 122 | currentHead.next = newHead; 123 | 124 | const newSnakeCells = new Set(snakeCells); 125 | newSnakeCells.delete(snake.tail.value.cell); 126 | newSnakeCells.add(nextHeadCell); 127 | 128 | snake.tail = snake.tail.next; 129 | if (snake.tail === null) snake.tail = snake.head; 130 | 131 | const foodConsumed = nextHeadCell === foodCell; 132 | if (foodConsumed) { 133 | // This function mutates newSnakeCells. 134 | growSnake(newSnakeCells); 135 | if (foodShouldReverseDirection) reverseSnake(); 136 | handleFoodConsumption(newSnakeCells); 137 | } 138 | 139 | setSnakeCells(newSnakeCells); 140 | }; 141 | 142 | // This function mutates newSnakeCells. 143 | const growSnake = newSnakeCells => { 144 | const growthNodeCoords = getGrowthNodeCoords(snake.tail, direction); 145 | if (isOutOfBounds(growthNodeCoords, board)) { 146 | // Snake is positioned such that it can't grow; don't do anything. 147 | return; 148 | } 149 | const newTailCell = board[growthNodeCoords.row][growthNodeCoords.col]; 150 | const newTail = new LinkedListNode({ 151 | row: growthNodeCoords.row, 152 | col: growthNodeCoords.col, 153 | cell: newTailCell, 154 | }); 155 | const currentTail = snake.tail; 156 | snake.tail = newTail; 157 | snake.tail.next = currentTail; 158 | 159 | newSnakeCells.add(newTailCell); 160 | }; 161 | 162 | const reverseSnake = () => { 163 | const tailNextNodeDirection = getNextNodeDirection(snake.tail, direction); 164 | const newDirection = getOppositeDirection(tailNextNodeDirection); 165 | setDirection(newDirection); 166 | 167 | // The tail of the snake is really the head of the linked list, which 168 | // is why we have to pass the snake's tail to `reverseLinkedList`. 169 | reverseLinkedList(snake.tail); 170 | const snakeHead = snake.head; 171 | snake.head = snake.tail; 172 | snake.tail = snakeHead; 173 | }; 174 | 175 | const handleFoodConsumption = newSnakeCells => { 176 | const maxPossibleCellValue = BOARD_SIZE * BOARD_SIZE; 177 | let nextFoodCell; 178 | // In practice, this will never be a time-consuming operation. Even 179 | // in the extreme scenario where a snake is so big that it takes up 90% 180 | // of the board (nearly impossible), there would be a 10% chance of generating 181 | // a valid new food cell--so an average of 10 operations: trivial. 182 | while (true) { 183 | nextFoodCell = randomIntFromInterval(1, maxPossibleCellValue); 184 | if (newSnakeCells.has(nextFoodCell) || foodCell === nextFoodCell) 185 | continue; 186 | break; 187 | } 188 | 189 | const nextFoodShouldReverseDirection = 190 | Math.random() < PROBABILITY_OF_DIRECTION_REVERSAL_FOOD; 191 | 192 | setFoodCell(nextFoodCell); 193 | setFoodShouldReverseDirection(nextFoodShouldReverseDirection); 194 | setScore(score + 1); 195 | }; 196 | 197 | const handleGameOver = () => { 198 | setScore(0); 199 | const snakeLLStartingValue = getStartingSnakeLLValue(board); 200 | setSnake(new LinkedList(snakeLLStartingValue)); 201 | setFoodCell(snakeLLStartingValue.cell + 5); 202 | setSnakeCells(new Set([snakeLLStartingValue.cell])); 203 | setDirection(Direction.RIGHT); 204 | }; 205 | 206 | return ( 207 | <> 208 |

Score: {score}

209 |
210 | {board.map((row, rowIdx) => ( 211 |
212 | {row.map((cellValue, cellIdx) => { 213 | const className = getCellClassName( 214 | cellValue, 215 | foodCell, 216 | foodShouldReverseDirection, 217 | snakeCells, 218 | ); 219 | return
; 220 | })} 221 |
222 | ))} 223 |
224 | 225 | ); 226 | }; 227 | 228 | const createBoard = BOARD_SIZE => { 229 | let counter = 1; 230 | const board = []; 231 | for (let row = 0; row < BOARD_SIZE; row++) { 232 | const currentRow = []; 233 | for (let col = 0; col < BOARD_SIZE; col++) { 234 | currentRow.push(counter++); 235 | } 236 | board.push(currentRow); 237 | } 238 | return board; 239 | }; 240 | 241 | const getCoordsInDirection = (coords, direction) => { 242 | if (direction === Direction.UP) { 243 | return { 244 | row: coords.row - 1, 245 | col: coords.col, 246 | }; 247 | } 248 | if (direction === Direction.RIGHT) { 249 | return { 250 | row: coords.row, 251 | col: coords.col + 1, 252 | }; 253 | } 254 | if (direction === Direction.DOWN) { 255 | return { 256 | row: coords.row + 1, 257 | col: coords.col, 258 | }; 259 | } 260 | if (direction === Direction.LEFT) { 261 | return { 262 | row: coords.row, 263 | col: coords.col - 1, 264 | }; 265 | } 266 | }; 267 | 268 | const isOutOfBounds = (coords, board) => { 269 | const {row, col} = coords; 270 | if (row < 0 || col < 0) return true; 271 | if (row >= board.length || col >= board[0].length) return true; 272 | return false; 273 | }; 274 | 275 | const getDirectionFromKey = key => { 276 | if (key === 'ArrowUp') return Direction.UP; 277 | if (key === 'ArrowRight') return Direction.RIGHT; 278 | if (key === 'ArrowDown') return Direction.DOWN; 279 | if (key === 'ArrowLeft') return Direction.LEFT; 280 | return ''; 281 | }; 282 | 283 | const getNextNodeDirection = (node, currentDirection) => { 284 | if (node.next === null) return currentDirection; 285 | const {row: currentRow, col: currentCol} = node.value; 286 | const {row: nextRow, col: nextCol} = node.next.value; 287 | if (nextRow === currentRow && nextCol === currentCol + 1) { 288 | return Direction.RIGHT; 289 | } 290 | if (nextRow === currentRow && nextCol === currentCol - 1) { 291 | return Direction.LEFT; 292 | } 293 | if (nextCol === currentCol && nextRow === currentRow + 1) { 294 | return Direction.DOWN; 295 | } 296 | if (nextCol === currentCol && nextRow === currentRow - 1) { 297 | return Direction.UP; 298 | } 299 | return ''; 300 | }; 301 | 302 | const getGrowthNodeCoords = (snakeTail, currentDirection) => { 303 | const tailNextNodeDirection = getNextNodeDirection( 304 | snakeTail, 305 | currentDirection, 306 | ); 307 | const growthDirection = getOppositeDirection(tailNextNodeDirection); 308 | const currentTailCoords = { 309 | row: snakeTail.value.row, 310 | col: snakeTail.value.col, 311 | }; 312 | const growthNodeCoords = getCoordsInDirection( 313 | currentTailCoords, 314 | growthDirection, 315 | ); 316 | return growthNodeCoords; 317 | }; 318 | 319 | const getOppositeDirection = direction => { 320 | if (direction === Direction.UP) return Direction.DOWN; 321 | if (direction === Direction.RIGHT) return Direction.LEFT; 322 | if (direction === Direction.DOWN) return Direction.UP; 323 | if (direction === Direction.LEFT) return Direction.RIGHT; 324 | }; 325 | 326 | const getCellClassName = ( 327 | cellValue, 328 | foodCell, 329 | foodShouldReverseDirection, 330 | snakeCells, 331 | ) => { 332 | let className = 'cell'; 333 | if (cellValue === foodCell) { 334 | if (foodShouldReverseDirection) { 335 | className = 'cell cell-purple'; 336 | } else { 337 | className = 'cell cell-red'; 338 | } 339 | } 340 | if (snakeCells.has(cellValue)) className = 'cell cell-green'; 341 | 342 | return className; 343 | }; 344 | 345 | export default Board; 346 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | 6 | ReactDOM.render( 7 | 8 | 9 | , 10 | document.getElementById('root') 11 | ); -------------------------------------------------------------------------------- /src/lib/utils.js: -------------------------------------------------------------------------------- 1 | import {useEffect, useRef} from 'react'; 2 | 3 | // Copied from https://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript 4 | export function randomIntFromInterval(min, max) { 5 | // min and max included 6 | return Math.floor(Math.random() * (max - min + 1) + min); 7 | } 8 | 9 | // Copied from https://overreacted.io/making-setinterval-declarative-with-react-hooks/ 10 | export function useInterval(callback, delay) { 11 | const savedCallback = useRef(); 12 | 13 | // Remember the latest callback. 14 | useEffect(() => { 15 | savedCallback.current = callback; 16 | }, [callback]); 17 | 18 | // Set up the interval. 19 | useEffect(() => { 20 | function tick() { 21 | savedCallback.current(); 22 | } 23 | if (delay !== null) { 24 | let id = setInterval(tick, delay); 25 | return () => clearInterval(id); 26 | } 27 | }, [delay]); 28 | } 29 | 30 | export function reverseLinkedList(head) { 31 | let previousNode = null; 32 | let currentNode = head; 33 | while (currentNode !== null) { 34 | const nextNode = currentNode.next; 35 | currentNode.next = previousNode; 36 | previousNode = currentNode; 37 | currentNode = nextNode; 38 | } 39 | return previousNode; 40 | } 41 | --------------------------------------------------------------------------------