[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/feixiangcode/algorithm/master/Week_01/id_101/LeetCode_24_101.java [Back]  [Original]

package linkedlist.swapNodesInPairs;

import linkedlist.ListNode;

/**
 * @author Seina
 * @version 2019-01-16 22:35:25
 */
public class SwapNodesInPairs {

    public ListNode swapPairs(ListNode head) {

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

        ListNode root = new ListNode();
        root.next = head;
        ListNode pre = root;
        ListNode a;
        ListNode b;

        //abpre
        while (pre.next != null && pre.next.next != null) {
            a = pre.next;
            b = a.next;
            pre.next = b;
            a.next = b.next;
            b.next = a;
            pre = a;
        }
        return root.next;
    }

}

Web Proxy Viewer  |  New URL  |  Original Page