From 0ced7cb8f3ecea49a066f61cceabd3e4f64aa64b Mon Sep 17 00:00:00 2001 From: TheGeniusOfEternity Date: Tue, 18 Aug 2026 12:48:57 +0300 Subject: [PATCH 1/3] test(neetcode|roadmap|reorder-list): testcases --- .../linked-list/reorder-list/solution.test.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/neetcode/linked-list/reorder-list/solution.test.ts diff --git a/src/neetcode/linked-list/reorder-list/solution.test.ts b/src/neetcode/linked-list/reorder-list/solution.test.ts new file mode 100644 index 0000000..410b901 --- /dev/null +++ b/src/neetcode/linked-list/reorder-list/solution.test.ts @@ -0,0 +1,25 @@ +import { createList, listToArray } from '../common'; +import { reorderList } from './solution'; + +describe('Reorder List | NeetCode | RoadMap | Testcases', () => { + test('#1', () => { + const head = createList([2, 4, 6, 8]); + const expected = [2, 8, 4, 6]; + reorderList(head); + expect(listToArray(head)).toEqual(expected); + }); + + test('#2', () => { + const head = createList([2, 4, 6, 8, 10]); + const expected = [2, 10, 4, 8, 6]; + reorderList(head); + expect(listToArray(head)).toEqual(expected); + }); + + test('#3', () => { + const head = createList([]); + const expected: number[] = []; + reorderList(head); + expect(listToArray(head)).toEqual(expected); + }); +}); From 44f65b87f695495064b38d62ca60291e4b715b02 Mon Sep 17 00:00:00 2001 From: TheGeniusOfEternity Date: Tue, 18 Aug 2026 12:49:11 +0300 Subject: [PATCH 2/3] feat(neetcode|roadmap|reorder-list): solution --- .../linked-list/reorder-list/solution.ts | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/neetcode/linked-list/reorder-list/solution.ts diff --git a/src/neetcode/linked-list/reorder-list/solution.ts b/src/neetcode/linked-list/reorder-list/solution.ts new file mode 100644 index 0000000..471660e --- /dev/null +++ b/src/neetcode/linked-list/reorder-list/solution.ts @@ -0,0 +1,50 @@ +import { ListNode } from '../common'; + +/** + * If a given linked list with head `head` of `length = n` is represented as: + * + * `[0, 1, ..., n - 2, n - 1]` + * + * Then function reorders its nodes to be in the following order: + * + * `[0, n-1, 1, n-2, 2, n-3, ...]` + * + * @param {ListNode} head - the head of a singly linked-list + * @return {void} + */ +export const reorderList = (head: ListNode | null): void => { + let slow: ListNode | null = head; + let fast: ListNode | null = head; + + while (fast !== null && fast.next !== null) { + slow = slow?.next ?? null; + fast = fast.next.next; + } + + let prev: ListNode | null = null; + let curr: ListNode | null = slow; + + while (curr !== null) { + const next = curr.next; + curr.next = prev; + prev = curr; + curr = next; + } + + let l1 = head; + let l2 = prev; + + while (l2 !== null && l1 !== null) { + const nextF = l1.next; + const nextS = l2.next; + l1.next = l2; + l2.next = nextF; + + l1 = nextF; + l2 = nextS; + } + + if (l1 !== null) { + l1.next = null; + } +}; From d791062dc75d511f901216e7b5857b636684480f Mon Sep 17 00:00:00 2001 From: TheGeniusOfEternity Date: Tue, 18 Aug 2026 12:49:19 +0300 Subject: [PATCH 3/3] feat(neetcode|roadmap|docs): update table of contents --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 668fa5d..67207c1 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ - [Task #34](src/neetcode/linked-list/reversed-linked-list) – _Reversed Linked List_ - _[check the task](https://neetcode.io/problems/reverse-a-linked-list)_ - [Task #35](src/neetcode/linked-list/merge-two-sorted-lists) – _Merge Two Sorted Lists_ - _[check the task](https://neetcode.io/problems/merge-two-sorted-linked-lists)_ - [Task #36](src/neetcode/linked-list/linked-list-cycle) – _Linked List Cycle_ - _[check the task](https://neetcode.io/problems/linked-list-cycle-detection)_ + - [Task #37](src/neetcode/linked-list/reorder-list) – _Reorder List_ - _[check the task](https://neetcode.io/problems/reorder-linked-list)_ [BigFrontEnd - JavaScript Coding Questions](https://bigfrontend.dev/problem) - [1. implement curry()](src/bigfrontend/javascript/curry) - _Curry Function_ - [check the task](https://bigfrontend.dev/problem/implement-curry)