-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeetCode_24.java
40 lines (33 loc) · 917 Bytes
/
LeetCode_24.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
public class LeetCode_24 {
public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null)
return null;
ListNode pre = new ListNode(0, head);
reverse(pre);
return pre.next;
}
public void reverse(ListNode node) {
if (node == null || node.next == null || node.next.next == null)
return;
reverse(node.next.next);
ListNode temp = node.next;
node.next = node.next.next;
temp.next = node.next.next;
node.next.next = temp;
}
}
}