Simple Java solution with three pointers


  • 0
    S
    public ListNode reverseList(ListNode head) {
            if(head==null || head.next==null) return head;
            ListNode prev=head, cur=head.next, next=cur.next;
            while(cur!=null) {
                cur.next=prev;
                prev=cur;
                cur=next;
                next=next!=null?next.next: null;
            }
            head.next=null;
            return prev;
        }
    

Log in to reply
 

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