Skip to content

Commit

Permalink
190th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Aug 10, 2024
1 parent 1644c98 commit e7091a6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | |
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
});
});
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit e7091a6

Please sign in to comment.