-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7091a6
commit 5ca0794
Showing
3 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |