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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@
- [Task #31](src/neetcode/binary-search/search-in-rotated-sorted-array) – _Search In Rotated Sorted Array_ - _[check the task](https://neetcode.io/problems/find-target-in-rotated-sorted-array)_
- [Task #32](src/neetcode/binary-search/time-based-key-value-store) – _Time Based Key Value Store_ - _[check the task](https://neetcode.io/problems/time-based-key-value-store)_
- [Task #33](src/neetcode/binary-search/median-of-two-sorted-arrays) – _Median Of Two Sorted Arrays_ - _[check the task](https://neetcode.io/problems/median-of-two-sorted-arrays)_
- Linked List
- [Task #34](src/neetcode/linked-list/reversed-linked-list) – _Reversed Linked List_ - _[check the task](https://neetcode.io/problems/reverse-a-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
59 changes: 59 additions & 0 deletions src/neetcode/linked-list/reversed-linked-list/solution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ListNode, reverseList } from './solution';

const createList = (values: number[]): ListNode | null => {
const dummy = new ListNode();
let tail = dummy;

for (const value of values) {
tail.next = new ListNode(value);
tail = tail.next;
}

return dummy.next;
};

const listToArray = (head: ListNode | null): number[] => {
const values: number[] = [];
let current = head;

while (current) {
values.push(current.val);
current = current.next;
}

return values;
};

describe('Reversed Linked List | NeetCode | RoadMap | Testcases', () => {
test('#1 Reverses a linked list', () => {
const head = createList([0, 1, 2, 3]);

expect(listToArray(reverseList(head))).toEqual([3, 2, 1, 0]);
});

test('returns null for an empty list', () => {
expect(reverseList(null)).toBeNull();
});

test('#2 Returns the same node for a single-node list', () => {
const head = new ListNode(42);

expect(reverseList(head)).toBe(head);
expect(head.next).toBeNull();
});

test('#3 Reverses the existing links instead of creating new nodes', () => {
const first = new ListNode(1);
const second = new ListNode(2);
const third = new ListNode(3);
first.next = second;
second.next = third;

const reversed = reverseList(first);

expect(reversed).toBe(third);
expect(third.next).toBe(second);
expect(second.next).toBe(first);
expect(first.next).toBeNull();
});
});
26 changes: 26 additions & 0 deletions src/neetcode/linked-list/reversed-linked-list/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export class ListNode {
val: number;
next: ListNode | null;
constructor(val = 0, next = null) {
this.val = val;
this.next = next;
}
}

/**
* @param {ListNode} head - the beginning of a singly linked list
* @return {ListNode} - the new beginning of the reversed list.
*/
export const reverseList = (head: ListNode | null): ListNode | null => {
let prev: ListNode | null = null;
let curr = head;

while (curr !== null) {
const temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}

return prev;
};
Loading