Monday, January 25, 2016

Reverse Linked List Node In Java

    //Utility function to reverse the given list
      //Ex: Input : 1->2->3
      //Output : 3->2->1
      static ListNode reverse(ListNode list){
             if(list == null)
                   return null;

             ListNode current = list, previous = null, forward;
             while(current != null){
                    forward = current.next;
                    current.next = previous;
                    previous = current;
                    current = forward;
             }
             return previous;
      }

No comments:

Post a Comment