leetcode

Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.

For example, Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

public ListNode swapPairs(ListNode head) {
        if (head==null||head.next==null) {
            return head;
        }

        ListNode newHead = head.next;
        ListNode previous = new ListNode(0);
        previous.next = head;

        while (head!=null && head.next!=null) {
            ListNode next = head.next;
            ListNode nextNext = next.next;

            //0 -> 2
            previous.next = next;
            //2 -> 1
            next.next = head;
            //1 -> 3
            head.next = nextNext;
            //now 1 is the previous node of the pair
            previous = head;
            //now 3 is the new head
            head = nextNext;
        }

        return newHead;

    }