LeetCode024 Swap Nodes in Pairs

详细见:leetcode.com/problems/swap-nodes-in-pairs/


Java Solution: github

package leetcode;


import tools.ListNode辅助.ListNode;

public class P024_SwapNodesInPairs {
	public static void main(String[] args) {
		ListNode input = tools.ListNode辅助.A_一维生成器(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
		tools.ListNode辅助.B_打印链表(new Solution2().swapPairs(input));
	}
	/*
	 * 	0 ms
	 * 	6.30%
	 */
	static class Solution1 {
	    public ListNode swapPairs(ListNode head) {
	    	if (head == null || head.next == null)
	    		return head;
	        ListNode pre = head, cur = pre.next.next, tmp = null, ans = head.next;
	        pre.next.next = head;
	        head.next = cur;
	        boolean isOdd = true;
	        while (cur != null) {
	        	if (! isOdd) {
	        		tmp = cur.next;
	        		cur.next = pre.next;
	        		pre.next.next = tmp;
	        		pre.next = cur;
	        		pre = cur.next;
	        		cur = pre.next;
	        	} else {
		        	cur = cur.next;
	        	}
	        	isOdd = ! isOdd;
	        }
	        return ans;
	    }
	}
	/*
	 * 	1的问题就是代码太麻烦
	 * 	一定要有更加简洁的代码
	 * 	0 ms
	 * 	6.30%
	 */
	static class Solution2 {
	    public ListNode swapPairs(ListNode head) {
	    	if (head == null || head.next == null)
	    		return head;
	        ListNode pre = head, cur = pre.next.next, next_cur = null, ans = head.next;
	        pre.next.next = head;
	        head.next = cur;
	        boolean isOdd = true;
	        while (cur != null) {
	        	if (! isOdd) {
	        		next_cur = cur.next;
	        		cur.next = pre.next;
	        		pre.next = cur;
	        		cur = cur.next;
	        		cur.next = next_cur;
	        		pre = cur;
	        		cur = next_cur;
	        	} else {
		        	cur = cur.next;
	        	}
	        	isOdd = ! isOdd;
	        }
	        return ans;
	    }
	}
}


C Solution: github

/*
    url: leetcode.com/problems/swap-nodes-in-pairs/
    6ms 0.0%
*/

#include <stdio.h>
#include <stdlib.h>

struct ListNode {
    int val;
    struct ListNode *next;
};

struct ListNode * convert_int_to_ListNode(int * arr, int n) {
    struct ListNode * head = NULL;
    struct ListNode * travel = NULL;
    struct ListNode * temp = NULL;
    int i = 0;
    if (n == 0 || n < 0) return NULL;
    travel = (struct ListNode *) malloc(sizeof(struct ListNode));
    travel->val = *(arr + 0);
    travel->next = NULL;
    head = travel;
    for (i = 1; i < n; i ++) {
        temp = (struct ListNode *) malloc(sizeof(struct ListNode));
        temp->val = *(arr + i);
        temp->next = NULL;
        travel->next = temp;
        travel = travel->next;
    }
    return head;
}

void free_ListNode(struct ListNode * l) {
    struct ListNode * temp = NULL;
    while (l != NULL) {
        temp = l->next;
        free(l);
        l = temp;
    }
}

void print_ListNode(struct ListNode * h) {
    while (h != NULL) {
        printf("%d ", h->val);
        h = h->next;
    }
    printf("\r\n");
}

struct ListNode* swapPairs(struct ListNode* head) {
    struct ListNode * t1 = head, * t2 = NULL, * t3 = NULL, * t0 = NULL;
    struct ListNode * answer = head;
    t2 = t1 == NULL ? NULL : t1->next;
    t3 = t2 == NULL ? NULL : t2->next;
    while (t2 != NULL) {
        t2->next = t1;
        if (t0 != NULL)
            t0->next = t2;
        if (head == answer)
            answer = t2;
        t0 = t1;
        t1 = t3;
        t2 = t1 == NULL ? NULL : t1->next;
        t3 = t2 == NULL ? NULL : t2->next;
    }
    if (t0 != NULL)
        t0->next = t1;
    return answer;
}

int main() {
    int a[] = {1};
    struct ListNode * l = convert_int_to_ListNode(a, 1);
    struct ListNode * answer = swapPairs(l);
    print_ListNode(answer);
    free_ListNode(answer);
}


Python Solution: github

#coding=utf-8

'''
    url: leetcode.com/problems/swap-nodes-in-pairs/
    @author:     zxwtry
    @email:      zxwtry@qq.com
    @date:       2017年3月31日
    @details:    Solution: 52ms 31.83%
'''

from leetcode.Utils import *

class Solution(object):
    def swapPairs(self, head):
        if head == None: return head
        ans = head.next if head.next != None else head
        t0, t1, t2, t3 = None, head, head.next, None if \
        head.next == None else head.next.next
        while t2 != None:
            t2.next = t1
            t1.next = t3
            if t0 != None:
                t0.next = t2
            t0, t1, t2 = t1, t3, None if t3 == None else t3.next
            t3 = None if t2 == None else t2.next
        return ans

if __name__ == "__main__":
    head = convertArrayToListNode([1, 2, 3, 4, 5, 6, 7, 8])
    sol = Solution()
    printListNode(sol.swapPairs(head))






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值