reverse of a linked list
Anonymous
Given a LinkedList of Node (val, next), below is an in-place reverse function: // Class: Node var Node = function(val) { this.val = val; this.next = null; }; // Assuming there is a linked-list with 2 nodes var reverse = function(linkedList) { var previous = linkedList.head, current = previous.next, next ; previous.next = null; while(current != null) { next = current.next; current.next = previous; previous = current; current = next; } linkedList.head = previous; };
Check out your Company Bowl for anonymous work chats.