-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
36 lines (32 loc) · 827 Bytes
/
Solution.java
File metadata and controls
36 lines (32 loc) · 827 Bytes
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
package leetCode_24;
/**
* @author dimdark
* @date 2017-10-08
* @time 9:02 PM
*/
public class Solution {
class ListNode {
int val;
ListNode next;
ListNode(int x) {
this.val = x;
}
}
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) return head;
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode node = dummyHead;
ListNode pNode, qNode, rNode;
while (node.next != null && node.next.next != null) {
pNode = node.next;
qNode = pNode.next;
rNode = qNode.next;
node.next = qNode;
qNode.next = pNode;
pNode.next = rNode;
node = pNode;
}
return dummyHead.next;
}
}