My Java Tail Recursion Solutin O(N)


  • 0
    K
    public ListNode reverseList(ListNode head) {
    	if(head==null) return null;
        return rec(null, head);
    }
    
    public ListNode rec(ListNode _pre, ListNode _suc){
    	ListNode next=_suc.next;
    	_suc.next=_pre;
    	return (next==null)?_suc:rec(_suc,next);
    }

Log in to reply
 

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