We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 17220e8 commit 5767efdCopy full SHA for 5767efd
kotlin/0024-swap-nodes-in-pairs.kt
@@ -0,0 +1,30 @@
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
+ var dummy = ListNode(0, head)
13
+ var prev = dummy
14
+ var cur = head
15
+
16
+ while (cur != null && cur.next != null) {
17
+ val next = cur.next
18
+ val nextNext = cur.next.next
19
20
+ next.next = cur
21
+ cur.next = nextNext
22
+ prev.next = next
23
24
+ prev = cur
25
+ cur = nextNext
26
+ }
27
28
+ return dummy.next
29
30
+}
0 commit comments