We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 984e1fb commit 8e6f00bCopy full SHA for 8e6f00b
0024-swap-nodes-in-pairs/0024-swap-nodes-in-pairs.kt
@@ -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