Skip to content

Commit

Permalink
191st Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Aug 11, 2024
1 parent e7091a6 commit 5ca0794
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | |
Expand Down
21 changes: 21 additions & 0 deletions src/page-4/328. Odd Even Linked List/oddEvenList.test.ts
Original file line number Diff line number Diff line change
@@ -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));
}
});
});
30 changes: 30 additions & 0 deletions src/page-4/328. Odd Even Linked List/oddEvenList.ts
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 5ca0794

Please sign in to comment.