Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions src/neetcode/linked-list/reorder-list/solution.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
50 changes: 50 additions & 0 deletions src/neetcode/linked-list/reorder-list/solution.ts
Original file line number Diff line number Diff line change
@@ -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;
}
};
Loading