Created
August 18, 2020 22:23
-
-
Save aamirafridi/7bc7eb48c36bfc6c958f2ccb4683f78a to your computer and use it in GitHub Desktop.
Reversing a doubly linked list in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverse(head) { | |
let nextNode = null; | |
let prevNode = null; | |
let currentNode = head; | |
while(currentNode) { | |
nextNode = currentNode.next; | |
currentNode.prev = nextNode; | |
currentNode.next = prevNode; | |
prevNode = currentNode; | |
currentNode = nextNode; | |
} | |
return prevNode | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment