From dfdf79cb84f655a319e559b02bdd2065b8a25db4 Mon Sep 17 00:00:00 2001 From: javaSwing Date: Wed, 22 Nov 2023 00:30:01 +0800 Subject: [PATCH] feat: complete 148 --- code/linked-list/book/linked-list.ts | 2 +- code/linked-list/design-linked-list.ts | 13 +--- code/linked-list/sort-list.ts | 63 +++++++++++++++++++ .../test/circular-linked-list.test.ts | 14 ++--- code/linked-list/test/sort-list.test.ts | 17 +++++ 5 files changed, 89 insertions(+), 20 deletions(-) create mode 100644 code/linked-list/sort-list.ts create mode 100644 code/linked-list/test/sort-list.test.ts diff --git a/code/linked-list/book/linked-list.ts b/code/linked-list/book/linked-list.ts index 3072a8d..0df51e2 100644 --- a/code/linked-list/book/linked-list.ts +++ b/code/linked-list/book/linked-list.ts @@ -4,7 +4,7 @@ import { type LinkedListInterface } from './type'; export default class LinkedList implements LinkedListInterface { protected count: number; - protected head: undefined | LinkedNode; + head: undefined | LinkedNode; protected eqFn: (a: T, b: T) => boolean; constructor(eq = defaultEquals) { diff --git a/code/linked-list/design-linked-list.ts b/code/linked-list/design-linked-list.ts index 0748059..180925a 100644 --- a/code/linked-list/design-linked-list.ts +++ b/code/linked-list/design-linked-list.ts @@ -1,20 +1,13 @@ -class LinkedNode { - val: number; - next: LinkedNode | undefined; - constructor(v) { - this.val = v; - this.next = undefined; - } -} - /** * 707. 设计链表 * 尝试了好多测试用例才通过 * @link https://leetcode.cn/problems/design-linked-list/description/ */ +import { LinkedNode } from './book/linked-node'; + class MyLinkedList { - header: LinkedNode | undefined; + header: LinkedNode | undefined; count: number; constructor() { diff --git a/code/linked-list/sort-list.ts b/code/linked-list/sort-list.ts new file mode 100644 index 0000000..c4aa0b5 --- /dev/null +++ b/code/linked-list/sort-list.ts @@ -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 | undefined) { + return mergeList(head); +}; + +// 递归分割 +function mergeList(head: LinkedNode | undefined): LinkedNode | undefined { + // 边界 + if (!head || !head.next) { + return head; + } + + // 快慢指针分割链表 + let fast: undefined | LinkedNode = 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 | undefined, r: LinkedNode | 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; +} diff --git a/code/linked-list/test/circular-linked-list.test.ts b/code/linked-list/test/circular-linked-list.test.ts index 4279dc8..32156da 100644 --- a/code/linked-list/test/circular-linked-list.test.ts +++ b/code/linked-list/test/circular-linked-list.test.ts @@ -1,5 +1,4 @@ -import CircularLinkedList from "../book/circular-linked-list/circular-linked-list"; - +import CircularLinkedList from '../book/circular-linked-list/circular-linked-list'; describe('circular-linked-list', () => { it('should create empty doubly linked list', () => { @@ -12,20 +11,17 @@ describe('circular-linked-list', () => { linkedList.push(2); linkedList.push(5); linkedList.push(7); - linkedList.insert(1, 1) - linkedList.insert(10, 0) - expect(linkedList.toString()).toBe("10,2,1,5,7"); + linkedList.insert(1, 1); + linkedList.insert(10, 0); + expect(linkedList.toString()).toBe('10,2,1,5,7'); }); - it('test removeAt doubly linked list', () => { const linkedList = new CircularLinkedList(); linkedList.push(2); linkedList.push(5); linkedList.push(7); linkedList.removeAt(1); - expect(linkedList.toString()).toBe("2,7"); + expect(linkedList.toString()).toBe('2,7'); }); - - }); diff --git a/code/linked-list/test/sort-list.test.ts b/code/linked-list/test/sort-list.test.ts new file mode 100644 index 0000000..1e25dff --- /dev/null +++ b/code/linked-list/test/sort-list.test.ts @@ -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(); + 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); + }); +});