-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path24.java
157 lines (143 loc) · 3.97 KB
/
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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
__________________________________________________________________________________________________
0ms
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
if (head==null||head.next==null) {
return head;
}else {
int val=head.val;
head.val=head.next.val;
head.next.val=val;
head.next.next=swapPairs(head.next.next);
}
return head;
}
}
__________________________________________________________________________________________________
0ms
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode n = new ListNode(0);
n.next = head;
ListNode pointer = n;
while (pointer != null && pointer.next != null && pointer.next.next != null){
swapTwoNodes(pointer, pointer.next, pointer.next.next);
pointer = pointer.next.next;
}
return n.next;
}
public void swapTwoNodes(ListNode node0, ListNode node1, ListNode node2){
node0.next = node2;
node1.next = node2.next;
node2.next = node1;
}
}
__________________________________________________________________________________________________
2ms
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode zero = new ListNode(0);
System.out.println(zero.next);
recursion(zero, null , head);
return zero.next;
}
public void recursion(ListNode first, ListNode last , ListNode odd){
if(odd == null){
return;
}
ListNode even = odd.next;
if (first.next == null){
System.out.println("223");
if (even == null){
first.next = odd;
System.out.println("22");
}
else{
first.next = even;
}
}
if (even == null){
return;
}
ListNode eventNext = even.next;
odd.next = eventNext;
even.next = odd;
if (last != null){
last.next = even;
}
recursion(first, odd, eventNext);
}
}
__________________________________________________________________________________________________
34164 kb
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode cur = head;
if(cur == null) {
return cur;
}
ListNode swap = head.next;
if(swap == null) {
return cur;
}
cur.next = swapPairs(swap.next);
swap.next = cur;
return swap;
}
}
__________________________________________________________________________________________________
34180 kb
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapHelper(ListNode node){
if(node == null) return null;
if(node.next == null) return node;
ListNode temp = node.next;
node.next = swapHelper(node.next.next);
temp.next = node;
return temp;
}
public ListNode swapPairs(ListNode head) {
return swapHelper(head);
}
}
__________________________________________________________________________________________________