-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
89 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { LinkedNode } from './book/linked-node'; | ||
|
||
/** | ||
* 148. 排序链表 | ||
* @param {ListNode} head | ||
* @return {ListNode} | ||
* | ||
* @link https://leetcode.cn/problems/sort-list/description/ | ||
* | ||
* 使用归并排序 | ||
*/ | ||
export const sortList = function (head: LinkedNode<number> | undefined) { | ||
return mergeList(head); | ||
}; | ||
|
||
// 递归分割 | ||
function mergeList(head: LinkedNode<number> | undefined): LinkedNode<number> | undefined { | ||
// 边界 | ||
if (!head || !head.next) { | ||
return head; | ||
} | ||
|
||
// 快慢指针分割链表 | ||
let fast: undefined | LinkedNode<number> = head, | ||
slow = head, | ||
prevSlow = head; | ||
while (fast && fast.next && slow.next) { | ||
prevSlow = slow; | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
} | ||
|
||
prevSlow.next = undefined; | ||
const l = head; | ||
const r = slow; | ||
|
||
return merge(mergeList(l), mergeList(r)); | ||
} | ||
// 归并 | ||
function merge(l: LinkedNode<number> | undefined, r: LinkedNode<number> | undefined) { | ||
const dumy = new LinkedNode(-1); | ||
let c = dumy; | ||
while (l && r) { | ||
if (l.val <= r.val) { | ||
c.next = l; | ||
l = l.next; | ||
} else { | ||
c.next = r; | ||
r = r.next; | ||
} | ||
c = c.next; | ||
} | ||
|
||
if (l) { | ||
c.next = l; | ||
} | ||
|
||
if (r) { | ||
c.next = r; | ||
} | ||
|
||
return dumy.next; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import LinkedList from '../book/linked-list'; | ||
import { sortList } from '../sort-list'; | ||
|
||
describe('sort-list test', () => { | ||
it('should create empty doubly linked list', () => { | ||
const linkedList = new LinkedList<number>(); | ||
linkedList.push(4); | ||
linkedList.push(2); | ||
linkedList.push(1); | ||
linkedList.push(3); | ||
expect(linkedList.toString()).toBe('4,2,1,3'); | ||
|
||
const sortHead = sortList(linkedList.head); | ||
|
||
expect(sortHead?.val).toBe(1); | ||
}); | ||
}); |