Skip to content

Commit

Permalink
feat: complete 148
Browse files Browse the repository at this point in the history
  • Loading branch information
javaswing committed Nov 21, 2023
1 parent 76f1cfb commit dfdf79c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 20 deletions.
2 changes: 1 addition & 1 deletion code/linked-list/book/linked-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type LinkedListInterface } from './type';

export default class LinkedList<T> implements LinkedListInterface<T> {
protected count: number;
protected head: undefined | LinkedNode<T>;
head: undefined | LinkedNode<T>;
protected eqFn: (a: T, b: T) => boolean;

constructor(eq = defaultEquals) {
Expand Down
13 changes: 3 additions & 10 deletions code/linked-list/design-linked-list.ts
Original file line number Diff line number Diff line change
@@ -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<number> | undefined;
count: number;

constructor() {
Expand Down
63 changes: 63 additions & 0 deletions code/linked-list/sort-list.ts
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;
}
14 changes: 5 additions & 9 deletions code/linked-list/test/circular-linked-list.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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<number>();
linkedList.push(2);
linkedList.push(5);
linkedList.push(7);
linkedList.removeAt(1);
expect(linkedList.toString()).toBe("2,7");
expect(linkedList.toString()).toBe('2,7');
});


});
17 changes: 17 additions & 0 deletions code/linked-list/test/sort-list.test.ts
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);
});
});

0 comments on commit dfdf79c

Please sign in to comment.