My simple recursive Java solution


  • 0
    A
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
    	public ListNode reverseList(ListNode curr){
        	if(curr==null) return curr;
        	ListNode rest = curr.next;
        	if(rest==null) return curr;
        	rest = reverseList(rest);
        	curr.next.next = curr;    //  Here is the trick
        	curr.next =null;          //
        	return rest;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.