My Java recursive solution

  • 49
    public class Solution {
        public ListNode reverseList(ListNode head) {
            if(head==null ||
                return head;
            ListNode newHead=reverseList(nextNode);
            return newHead;

  • 2

    The definition of "nextNode" here is unnecessary, isn't it?

  • 1

    the nextNode is the key pointer for reversing the list. Newhead just keep track of the last node, make sure it's the new head node.

  • 3
    P; can
    just be = head;

    so nextNode is kinda unnecessary

  • 1

    very clear, it's more clear than any other codes I have seen.

  • 2

    Marvellous!!! How did you even think of this... especially using the outer layer to overwrite the inner layer!!! Fantastic.

  • 2

    nice solution. not very intuitive though.
    hard to get your head around this one.

  • 0

    Thanks for sharing. This is a very nice solution. On the contrary, I actually think this is a very intuitive solution. Storing before recursing is very clever. I attempted to write a recursive solution like this but did not came up with this optimization.

Log in to reply

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