Java optimal clean code


  • 0
    public class Solution {
        public ListNode swapPairs(ListNode head) {
            ListNode newHead = new ListNode(0);
            newHead.next = head;
            ListNode prev, curr, next;
            prev = newHead;
            curr = head;
            while (curr != null && curr.next != null) {
                next = curr.next;
                curr.next = next.next;
                next.next = prev.next;
                prev.next = next;
                prev = curr;
                curr = prev.next;
            }
            return newHead.next;
        }
    }

Log in to reply
 

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