Author: techfox9

Reverse a linked list, recursive method

Thursday, January 29th, 2009 @ 4:01 pm

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;
}

Algorithms


 


Comments are closed.