Bazaarvoice Interview Question

reverse a linked list

Interview Answer

Anonymous

Dec 16, 2014

One of many possible ways of doing this if(head == null || head.next == null) { return head; } Node second = head.next; Node third = second.next; head.next = null; second.next = head; if(third == null) { head = second; return head; } Node current = third; Node previous = second; while(current != null) { Node temp = current.next; current.next = previous; previous = current; current = temp; } head = previous; return head;