Easy Java solution


  • 1
    B

    Every iteration, move one to the head, until to the tail of list.

    public class Solution {
        public ListNode reverseList(ListNode head) {
            if (head == null || head.next == null) return head;
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            ListNode pre = head;
            ListNode p = pre.next;
            
            while (p != null) {
                pre.next = p.next;
                p.next = dummy.next;
                dummy.next = p;
                p = pre.next;
            }
            return dummy.next;
        }
    }
    

Log in to reply
 

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