diff --git a/README.md b/README.md index 5dedb1c..20af17f 100644 --- a/README.md +++ b/README.md @@ -160,11 +160,12 @@ Ace Coding Interview with 75 Qs | Linked List | | | | --------------------------------------------- | ---------------- | ------ | | 2095. Delete the Middle Node of a Linked List | [Solution][2095] | Medium | -| 328. Odd Even Linked List | Solution | Medium | +| 328. Odd Even Linked List | [Solution][328] | 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 +[328]: ./src/page-4/328.%20Odd%20Even%20Linked%20List/oddEvenList.ts [206]: ./src/page-2/206.%20Reverse%20Linked%20List/reverseList.ts | Binary Tree - DFS | | | diff --git a/src/page-4/328. Odd Even Linked List/oddEvenList.test.ts b/src/page-4/328. Odd Even Linked List/oddEvenList.test.ts new file mode 100644 index 0000000..efba806 --- /dev/null +++ b/src/page-4/328. Odd Even Linked List/oddEvenList.test.ts @@ -0,0 +1,21 @@ +import { stringify } from 'flatted'; + +import { generateLinkedList } from '~/utils/linked-list'; + +import { oddEvenList } from './oddEvenList'; + +describe('328. Odd Even Linked List', () => { + test('oddEvenList', () => { + { + const head = generateLinkedList([1, 2, 3, 4, 5]); + const expected = generateLinkedList([1, 3, 5, 2, 4]); + expect(stringify(oddEvenList(head))).toBe(stringify(expected)); + } + + { + const head = generateLinkedList([2, 1, 3, 5, 6, 4, 7]); + const expected = generateLinkedList([2, 3, 6, 7, 1, 5, 4]); + expect(stringify(oddEvenList(head))).toBe(stringify(expected)); + } + }); +}); diff --git a/src/page-4/328. Odd Even Linked List/oddEvenList.ts b/src/page-4/328. Odd Even Linked List/oddEvenList.ts new file mode 100644 index 0000000..aa33305 --- /dev/null +++ b/src/page-4/328. Odd Even Linked List/oddEvenList.ts @@ -0,0 +1,30 @@ +import type { ListNode } from '~/utils/linked-list'; + +type OddEvenList = (head: ListNode | null) => ListNode | null; + +/** + * Accepted + */ +export const oddEvenList: OddEvenList = (head) => { + // If the list is empty, return it as is + if (head === null) return head; + + // Initialize pointers for odd and even nodes + let odd = head; // Start odd pointer at the first node + let even = head.next; // Start even pointer at the second node + const evenHead = even; // Save the start of the even list + + // Traverse the list to rearrange the odd and even nodes + while (even !== null && even.next !== null) { + odd.next = even.next; // Link current odd node to the next odd node + odd = odd.next; // Move the odd pointer to the next odd node + even.next = odd.next; // Link current even node to the next even node + even = even.next; // Move the even pointer to the next even node + } + + // After the loop, connect the end of the odd list to the head of the even list + odd.next = evenHead; + + // Return the head of the reordered list + return head; +};