Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8e6f00b

Browse files
Time: 0 ms (100%), Space: 41.2 MB (7.65%) - LeetHub
1 parent 984e1fb commit 8e6f00b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Example:
3+
* var li = ListNode(5)
4+
* var v = li.`val`
5+
* Definition for singly-linked list.
6+
* class ListNode(var `val`: Int) {
7+
* var next: ListNode? = null
8+
* }
9+
*/
10+
class Solution {
11+
fun swapPairs(head: ListNode?): ListNode? {
12+
val ret = head?.next
13+
ret ?: return head
14+
15+
var prev: ListNode? = null
16+
var odd: ListNode? = null
17+
var now = head
18+
while (now != null) {
19+
if (odd == null) {
20+
odd = now
21+
22+
now = now.next
23+
} else {
24+
prev?.next = now
25+
odd.next = now.next
26+
now.next = odd
27+
28+
now = odd.next
29+
30+
prev = odd
31+
odd = null
32+
}
33+
}
34+
35+
return ret
36+
}
37+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /