-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathSwap Nodes in Pairs.java
executable file
·70 lines (59 loc) · 1.56 KB
/
Swap Nodes in Pairs.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
M
1520834172
tags: Linked List
#### enumurate
基本原理, 写出来, 然后连线:
pre -> A -> B -> C -> ...
需要一个虚拟 preNode做起始node, 不然无法把后面的node换到开头.
#### 注意
用dummy = pre作为head前一格.
用 `pre.next == null && pre.next.next` 来check是否为NULL.
pre.next.next 保证了至少有一次swap.
```
/*
Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
Example
Given 1->2->3->4, you should return the list as 2->1->4->3.
Challenge
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
Tags Expand
Linked List
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/*
Thoughts:
pre -> A -> B -> C -> ...
1. Link pre to B
2. Link A to C
3. Link B to A
4. move forward.
*/
class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode pre = new ListNode(-1);
pre.next = head;
ListNode dummy = pre;
while (pre.next != null && pre.next.next != null) {
ListNode a = pre.next;
ListNode b = a.next;
a.next = b.next;
b.next = a;
pre.next = b;
// Move
pre = pre.next.next;
}
return dummy.next;
}
}
```