My Java solution with three pointers.


  • 1
    C
    public ListNode reverseList(ListNode head) {
        if (head == null)
    		return null;
    	if (head.next == null)
    		return head;
    	ListNode pre = null;
    	ListNode cur = head;
    	ListNode next = head.next;
    	while (next != null) {
    		cur.next = pre;
    		pre = cur;
    		cur = next;
    		next = next.next;
    	}
    	cur.next = pre;
    	return cur;
    }

Log in to reply
 

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