Leetcode - Swap Nodes in Pairs

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:

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 -
    //www.greatytc.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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容