From e7091a6acf5304874087968003977085e3f8599d Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Sat, 10 Aug 2024 17:14:18 +0800 Subject: [PATCH] 190th Commit --- README.md | 15 +++++----- .../deleteMiddle.test.ts | 27 ++++++++++++++++++ .../deleteMiddle.ts | 28 +++++++++++++++++++ 3 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 src/page-19/2095. Delete the Middle Node of a Linked List/deleteMiddle.test.ts create mode 100644 src/page-19/2095. Delete the Middle Node of a Linked List/deleteMiddle.ts diff --git a/README.md b/README.md index 128635d..5dedb1c 100644 --- a/README.md +++ b/README.md @@ -157,13 +157,14 @@ Ace Coding Interview with 75 Qs [933]: ./src/page-9/933.%20Number%20of%20Recent%20Calls/RecentCounter.ts [649]: ./src/page-6/649.%20Dota2%20Senate/predictPartyVictory.ts -| Linked List | | | -| --------------------------------------------- | --------------- | ------ | -| 2095. Delete the Middle Node of a Linked List | Solution | Medium | -| 328. Odd Even Linked List | Solution | Medium | -| 206. Reverse Linked List | [Solution][206] | Easy | -| 2130. Maximum Twin Sum of a Linked List | Solution | Medium | - +| Linked List | | | +| --------------------------------------------- | ---------------- | ------ | +| 2095. Delete the Middle Node of a Linked List | [Solution][2095] | Medium | +| 328. Odd Even Linked List | Solution | Medium | +| 206. Reverse Linked List | [Solution][206] | Easy | +| 2130. Maximum Twin Sum of a Linked List | Solution | Medium | + +[2095]: ./src/page-19/2095.%20Delete%20the%20Middle%20Node%20of%20a%20Linked%20List/deleteMiddle.ts [206]: ./src/page-2/206.%20Reverse%20Linked%20List/reverseList.ts | Binary Tree - DFS | | | diff --git a/src/page-19/2095. Delete the Middle Node of a Linked List/deleteMiddle.test.ts b/src/page-19/2095. Delete the Middle Node of a Linked List/deleteMiddle.test.ts new file mode 100644 index 0000000..2bcde4f --- /dev/null +++ b/src/page-19/2095. Delete the Middle Node of a Linked List/deleteMiddle.test.ts @@ -0,0 +1,27 @@ +import { stringify } from 'flatted'; + +import { generateLinkedList } from '~/utils/linked-list'; + +import { deleteMiddle } from './deleteMiddle'; + +describe('2095. Delete the Middle Node of a Linked List', () => { + test('deleteMiddle', () => { + { + const head = generateLinkedList([1, 3, 4, 7, 1, 2, 6]); + const expected = generateLinkedList([1, 3, 4, 1, 2, 6]); + expect(stringify(deleteMiddle(head))).toBe(stringify(expected)); + } + + { + const head = generateLinkedList([1, 2, 3, 4]); + const expected = generateLinkedList([1, 2, 4]); + expect(stringify(deleteMiddle(head))).toBe(stringify(expected)); + } + + { + const head = generateLinkedList([2, 1]); + const expected = generateLinkedList([2]); + expect(stringify(deleteMiddle(head))).toBe(stringify(expected)); + } + }); +}); diff --git a/src/page-19/2095. Delete the Middle Node of a Linked List/deleteMiddle.ts b/src/page-19/2095. Delete the Middle Node of a Linked List/deleteMiddle.ts new file mode 100644 index 0000000..4147fdb --- /dev/null +++ b/src/page-19/2095. Delete the Middle Node of a Linked List/deleteMiddle.ts @@ -0,0 +1,28 @@ +import type { ListNode } from '~/utils/linked-list'; + +type DeleteMiddle = (head: ListNode | null) => ListNode | null; + +/** + * Accepted + */ +export const deleteMiddle: DeleteMiddle = (head) => { + // If the list is empty or has only one node, return null (no middle to remove) + if (head === null || head.next === null) return null; + + let slow: ListNode | null = head; + let fast: ListNode | null = head; + let prev: ListNode | null = null; + + while (fast !== null && fast.next !== null) { + prev = slow; + slow = slow?.next || null; + fast = fast.next.next; + } + + // 'slow' is now pointing to the middle node + if (prev !== null && slow !== null) { + prev.next = slow.next; // Remove the middle node + } + + return head; +};