From 1ebf877b0087ce79fe1142e385a0d5e1ae4ff249 Mon Sep 17 00:00:00 2001 From: xuzhiwei Date: Sat, 18 May 2019 19:11:50 +0900 Subject: [PATCH] add typescript supported --- typescript/06_linkedlist/SingleLinkedList.ts | 4 +- typescript/07_linkedlist/LinkedListAlog.ts | 134 +++++++++++++++++++ 2 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 typescript/07_linkedlist/LinkedListAlog.ts diff --git a/typescript/06_linkedlist/SingleLinkedList.ts b/typescript/06_linkedlist/SingleLinkedList.ts index bf15e76..5be3c92 100644 --- a/typescript/06_linkedlist/SingleLinkedList.ts +++ b/typescript/06_linkedlist/SingleLinkedList.ts @@ -103,8 +103,8 @@ class SingleLinkedList implements List { } class SingleNode { - value: T - next: SingleNode | null + public value: T + public next: SingleNode | null constructor(value: T, next: SingleNode | null = null) { this.value = value diff --git a/typescript/07_linkedlist/LinkedListAlog.ts b/typescript/07_linkedlist/LinkedListAlog.ts new file mode 100644 index 0000000..68b1178 --- /dev/null +++ b/typescript/07_linkedlist/LinkedListAlog.ts @@ -0,0 +1,134 @@ +/** + * 单链表的常见操作包括 + * 链表反转 + * 链表中环的检测 + * 有序链表的合并 + * 删除链表倒数第n个节点 + * 链表中间节点 + */ +class LinkedListAlog { + /** + * 反转链表,依次将节点插入到头部 + * @param list + */ + public static reverse(list: SingleNode): SingleNode | null { + let currentNode: SingleNode | null = list + let prevNode = null + while (currentNode) { + const nextNode: SingleNode | null = currentNode.next + currentNode.next = prevNode + prevNode = currentNode + currentNode = nextNode + } + return prevNode + } + + /** + * 通过快慢指针来检测是否为一个环 + * @param list + */ + public static checkCircle(list: SingleNode): boolean { + if (!list) return false + let fast: SingleNode | null = list.next + let slow: SingleNode | null = list + while (fast && fast.next) { + fast = fast.next.next + slow = slow!!.next + if (fast === slow) return true + } + return false + } + + /** + * 倒序删除节点 + * @param list + * @param index + */ + public static removeFromEnd(list: SingleNode, index: number): SingleNode | null { + // 如果是个环,就没必要找了 + if (this.checkCircle(list)) return list + let newNode = this.reverse(list) + let preNode = null + let pos = 0 + while (newNode && pos !== index) { + newNode = newNode.next + pos++ + preNode = newNode + } + if (!newNode) return null + if (preNode) { + preNode.next = newNode.next + } + return this.reverse(newNode) + } + + public static findMidNode(list: SingleNode): SingleNode | null { + if (!list) return null + let fast = list.next + let slow = list + while (fast && fast.next) { + fast = fast.next.next + slow = slow.next!! + } + return slow + } + + /** + * 有序链表的合并,根据不同的值进行插入 + * @param a + * @param b + */ + public static mergeSortedLists(a: SingleNode, b: SingleNode): SingleNode | null { + if (!a || !b) return a ? a : b + let p: SingleNode | null = a + let q: SingleNode | null = b + // 新链表的头部指针 + let newList: SingleNode | null = null + if (p.value < q.value) { + newList = p + p = p.next + } else { + newList = q + q = q.next + } + let currNode = newList + while (p && q) { + if (p.value < q.value) { + currNode.next = p + p = p.next + } else { + currNode.next = q + q = q.next + } + currNode = currNode.next + } + if (p) { + currNode.next = p + } else { + currNode.next = q + } + return newList + } +} + +class SingleNode { + public value: T + public next: SingleNode | null + + constructor(value: T, next: SingleNode | null = null) { + this.value = value + this.next = next + } +} + + +const node1 = new SingleNode(1) +node1.next = new SingleNode(3) +node1.next.next = new SingleNode(5) + +const node2 = new SingleNode(2) +node2.next = new SingleNode(7) +node2.next.next = new SingleNode(8) +node2.next.next.next = new SingleNode(10) + +console.log(LinkedListAlog.findMidNode(node1))