My recursive solution in Java


  • 1
    W
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        ListNode h=null;
        public ListNode reverseList(ListNode head) {
            if(head==null)return null;
            f(head);
            return h;
        }
        ListNode f(ListNode n){
            if(n.next!=null){
                ListNode r=f(n.next);
                r.next=n;
                n.next=null;
                return n;
            } else {
                h=n;
                return n;
            }
        }
    }

Log in to reply
 

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