Skip to content

Commit

Permalink
reverse linked list using iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
Abid Khan committed Nov 16, 2022
1 parent 7338cc4 commit d99c057
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions linkedlists/reverseLinkedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const ll = require('./linkedLists');
for(let i = 0; i< 10; i++){
ll.append(i+1)
}
ll.print()

function reverseLL(head){
let current = head;
let prev = null;
let fwd= null;

while(current){
fwd = current.next;
current.next = prev;
prev = current;
current = fwd
}

head = prev;
return head;
}

let head = reverseLL(ll.getHead())
let output = []
while(head){
output.push(head.element)
head = head.next
}

console.log(output.join("->"))

0 comments on commit d99c057

Please sign in to comment.