Skip to content

Commit 432377f

Browse files
committed
✨ sort list
1 parent 3bd209c commit 432377f

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

CATEGORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
- 8.哈希表
2323
781 523
2424

25+
- 9.链表
26+
148
27+
2528
* 字符串
2629

2730
* 排序

src/148-sort-list/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
var sortList = function (head) {
13+
let slow = head;
14+
let fast = head;
15+
16+
while (slow) {
17+
while (fast) {
18+
if (slow.val > fast.val) {
19+
const temp = slow.val;
20+
slow.val = fast.val;
21+
fast.val = temp;
22+
}
23+
fast = fast.next;
24+
}
25+
slow = slow.next;
26+
fast = slow;
27+
}
28+
29+
return head;
30+
};

0 commit comments

Comments
 (0)