Movatterモバイル変換


[0]ホーム

URL:


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

Commit3c77de0

Browse files
authored
Create 1721swapping-nodes-in-a-linked-list.kt
1 parente38bc7a commit3c77de0

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
classSolution {
11+
funswapNodes(head:ListNode?,k:Int):ListNode? {
12+
var cur= head
13+
repeat(k-1) { cur= cur?.next }
14+
15+
var left= cur
16+
17+
var right= head
18+
while (cur?.next!=null) {
19+
cur= cur?.next
20+
right= right?.next
21+
}
22+
23+
left?.`val`= right?.`val`.also { right?.`val`= left?.`val` }
24+
return head
25+
}
26+
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp