My Simple Java Recursive approach


  • 1
    K
    public class Solution {
        public ListNode reverse(ListNode head, ListNode prev) {
            if(head == null) return prev;
            
            ListNode newHead = reverse(head.next, head);
            head.next = prev;
            return newHead;
        }
        public ListNode reverseList(ListNode head) {
            return reverse(head, null);
        }
    }

Log in to reply
 

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