Skip to content
This repository was archived by the owner on Oct 3, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
- ShivayeModi
- Vishvesh Trivedi
- Fahri Gunadi
- ekoyanu99
- ekoyanu99
- Bagus
39 changes: 39 additions & 0 deletions src/Javascript/ReverseLinkedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class ListNode {
constructor(val, next = null) {

this.val = val;
this.next = next;
}
}

const linkedList = [5, 4, 3, 2, 1].reduce((acc, val) => new ListNode(val, acc), null);


const printList = (head) => {
if(!head) {
return;
}

console.log(head.val);
printList(head.next);
}

// --------- solution -----------

var reverseList = function(head) {
let prev = null;
let current = head;

while(current) {
let nextTemp = current.next;
current.next = prev;
prev = current;
current = nextTemp;
}

return prev;
};

printList(linkedList);
console.log('after reverse')
printList(reverseList(linkedList))