LeetCode: Swap Nodes in Pairs

LeetCode: Swap Nodes in Pairs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

/**
* Created by hzhou on 2015/5/21.
* Email: [email protected]
* <p>
* Given 1->2->3->4, you should return the list as 2->1->4->3.
*/
public class SwapNodesInPairs {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode preHead = new ListNode(0);
preHead.next = head;
ListNode pre = preHead;
ListNode x, y;
x = head;
y = head.next;
while (x != null && y != null) {
x.next = y.next;
pre.next = y;
y.next = x;
pre = x;
x = x.next;
y = x != null ? x.next : null;
}
return preHead.next;
}
@Test
public void test() {
ListNode l = new ListNode(1);
l.next = new ListNode(2);
l.next.next = new ListNode(3);
l.next.next.next = new ListNode(4);
l.next.next.next.next = new ListNode(5);
ListNode result = swapPairs(l);
}
}