Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5c4d600

Browse files
committed
feat: add leetcode question with kotlin #25
1 parent f10a450 commit 5c4d600

File tree

1 file changed

+51
-0
lines changed
  • 00-code(源代码)/src/com/hi/dhl/algorithms/leetcode/_025/kotlin

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.hi.dhl.algorithms.leetcode._025.kotlin
2+
3+
import com.hi.dhl.algorithms.model.ListNode
4+
5+
/**
6+
* <pre>
7+
* author: dhl
8+
* date : 2022/6/24
9+
* desc :
10+
* </pre>
11+
*/
12+
class Solution {
13+
14+
fun reverseKGroup(head: ListNode?, k: Int): ListNode? {
15+
if (head == null) {
16+
return head
17+
}
18+
val len = getListLength(head)
19+
val dumpHead = ListNode(0)
20+
dumpHead.next = head
21+
var p = dumpHead
22+
var q = dumpHead.next
23+
var index = 0
24+
while (q != null) {
25+
if (len - index < k) {
26+
return dumpHead.next
27+
}
28+
index = index + k
29+
var m = k;
30+
while (--m != 0) {
31+
val node = q.next
32+
q.next = q.next.next
33+
node.next = p.next
34+
p.next = node
35+
}
36+
p = q
37+
q = q.next
38+
}
39+
return dumpHead.next
40+
}
41+
42+
private fun getListLength(head: ListNode?): Int {
43+
var cur = head
44+
var len = 0
45+
while (cur != null) {
46+
cur = cur?.next
47+
len++
48+
}
49+
return len
50+
}
51+
}

0 commit comments

Comments
 (0)