Recursive solution easy to understand :)>


  • 0
    L
    public class Solution {
        public ListNode reverseList(ListNode head) {
            if(head == null || head.next == null){
                return head;
            }
            
            ListNode dummy = new ListNode(0);
            ListNode last = reverse(head, dummy);
            
            last.next = null;
            return dummy.next;
        }
        
        public ListNode reverse(ListNode head, ListNode dummy){
            if(head.next == null){
                dummy.next = head;
                return head;
            }
            
            ListNode ln = reverse(head.next, dummy);
            ln.next = head;
            return head;
        }
    }

Log in to reply
 

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