详细见: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;
}
}
}
/*
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);
}
#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))