Leetcode - Swap Nodes in Pairs

Leetcode - Swap Nodes in Pairs_第1张图片

My code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null)
            return head;
        ListNode dummy = new ListNode(Integer.MIN_VALUE);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode currL = pre.next;
        ListNode currR = currL.next;
        
        while (pre != null) {
            if (pre.next == null)
                break;
            else
                currL = pre.next;
            if (currL.next == null)
                break;
            else
                currR = currL.next;
            ListNode temp = currR.next;
            pre.next = currR;
            currR.next = currL;
            currL.next = temp;
            pre = currL;
        }
        return dummy.next;
    }
}

My test result:

Leetcode - Swap Nodes in Pairs_第2张图片
Paste_Image.png

这道题目没什么好说的,虽说是medium,但是挺简单。感觉写过insertion sort list 之后,链表类的简单题,中等题都不虚了。。。

感觉代码写的还不是很好。让人感觉太多判断,不顺其自然。
改写了下,如下:

public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null)
            return head;
        ListNode dummy = new ListNode(Integer.MIN_VALUE);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode slow = pre.next;
        ListNode fast = slow.next;
        while (fast != null) {
            ListNode temp = fast.next;
            pre.next = fast;
            fast.next = slow;
            slow.next = temp;
            pre = slow;
            slow = pre.next;
            if (slow == null)
                break;
            fast = slow.next;
        }
        return dummy.next;
    }

**
总结: Linkedlist, swap nodes
**

Anyway, Good luck, Richardo!

其实就是

  1. Reverse Nodes in k-Group -
    http://www.jianshu.com/p/25f5269c729d
    的特殊情况。
    思路很简单,没什么好说的。

My code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null)  
            return head;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode tail = head;
        ListNode pre = dummy;
        int counter = 0;
        while (tail != null) {
            counter++;
            if (counter >= 2) {
                ListNode temp = tail.next;
                /** swap these two nodes */
                tail.next = head;
                head.next = temp;
                pre.next = tail;
                pre = head;
                head = temp;
                tail = temp;
                counter = 0;
            }
            else
                tail = tail.next;
        }
        return dummy.next;
    }
}

Anyway, Good luck, Richardo!

My code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        } 
        
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode p1 = dummy.next;
        ListNode p2 = p1;
        ListNode pre = dummy;
        
        while (p1 != null && p1.next != null) {
            p2 = p1.next;
            ListNode temp = p2.next;
            pre.next = p2;
            p2.next = p1;
            p1.next = temp;
            pre = p1;
            p1 = temp;
            p2 = temp;
        }
        
        return dummy.next;
    }
}

差不多的解法。

Anyway, Good luck, Richardo! -- 08/15/2016

你可能感兴趣的:(Leetcode - Swap Nodes in Pairs)