Tail recursive version that should eliminate the O(n) storage for stack frames.


  • 0
    B

    Here is a tail recursive version that should eliminate the O(n) storage for stack frames.

    public class Solution {
        public ListNode ReverseList(ListNode head) {
            return ReverseList(head, null);
        }
        
        public ListNode ReverseList(ListNode head, ListNode prevHead){
            if(head == null) return prevHead;
            ListNode next = head.next;
            head.next = prevHead;
            return ReverseList(next, head);
        }
    }

Log in to reply
 

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