Archive for the 'Algorithms' Category

 

Reverse a linked list, recursive method

Jan 29, 2009 in Algorithms

from
http://javadecodedquestions.blogspot.com/2013/01/java-interviews-frequently-asked-puzzles.html


public static ListNode reverse (ListNode list) {
   if (list == null)
      return null;
   if (list.next == null)
      return list;
   ListNode secondElem = list.next;
   list.next = null;
   ListNode reverseRest = reverse(secondElem);
   secondElem.next = list;
   return reverseRest;
}