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

Commit114b7c5

Browse files
authored
Create 0148-sort-list.kt
1 parent9acaf25 commit114b7c5

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

‎kotlin/0148-sort-list.kt‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
classSolution {
2+
funsortList(head:ListNode?):ListNode? {
3+
4+
funmid(root:ListNode?):ListNode? {
5+
var slow= head
6+
var fast= head
7+
var prev:ListNode?=null
8+
while (fast!=null&& fast?.next!=null) {
9+
prev= slow
10+
slow= slow?.next
11+
fast= fast?.next?.next
12+
}
13+
prev?.next=null
14+
return slow
15+
}
16+
17+
funmerge(_l1:ListNode?,_l2:ListNode?):ListNode? {
18+
var l1=_l1
19+
var l2=_l2
20+
var dummy=ListNode()
21+
var tail= dummy
22+
23+
while (l1!=null&& l2!=null) {
24+
if (l1.value< l2.value) {
25+
tail.next= l1
26+
l1= l1?.next
27+
}else {
28+
tail.next= l2
29+
l2= l2?.next
30+
}
31+
tail= tail?.next
32+
}
33+
34+
if (l1!=null) tail.next= l1
35+
if (l2!=null) tail.next= l2
36+
37+
return dummy.next
38+
}
39+
40+
if (head==null|| head.next==null)
41+
return head
42+
43+
val mid= mid(head)
44+
val sortLeft= sortList(head)
45+
val sortRight= sortList(mid)
46+
47+
return merge(sortLeft, sortRight)
48+
}
49+
50+
valListNode.value
51+
get()=this.`val`
52+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp