-
Notifications
You must be signed in to change notification settings - Fork 57
Feat/rotate linked list #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ayushHardeniya
merged 2 commits into
HarshitPachori:main
from
Shuklax:feat/rotate-linked-list
Nov 1, 2025
+157
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,76 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| */ | ||
| export class ListNode { | ||
| /** | ||
| * Creates a new ListNode. | ||
| * @param {number} value - The value of the node | ||
| * @param {ListNode|null} next - The reference to the next node (default is null) | ||
| */ | ||
| constructor(value = 0, next = null) { | ||
| this.value = value; | ||
| this.next = next; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Groups all nodes with odd indices together followed by even indices. | ||
| * @param {ListNode} head - The head of the linked list | ||
| * @returns {ListNode} - The reordered linked list | ||
| */ | ||
| export function oddEvenLinkedList(head) { | ||
| if (!head || !head.next) return head; | ||
|
|
||
| let odd = head; | ||
| let even = head.next; | ||
| const evenHead = even; | ||
|
|
||
| while (even && even.next) { | ||
| odd.next = even.next; | ||
| odd = odd.next; | ||
| even.next = odd.next; | ||
| even = even.next; | ||
| } | ||
|
|
||
| // Link the end of odd list to the start of even list | ||
| odd.next = evenHead; | ||
|
|
||
| return head; | ||
| } | ||
|
|
||
| /** | ||
| * Helper function to print the linked list (for testing purposes). | ||
| * @param {ListNode} head - The head of the linked list | ||
| */ | ||
| export function printList(head) { | ||
| const values = []; | ||
| let current = head; | ||
| while (current) { | ||
| values.push(current.value); | ||
| current = current.next; | ||
| } | ||
| console.log(values.join(" -> ")); | ||
| } | ||
|
|
||
| // Tiny self-check in browser/console environment | ||
| if (typeof window !== "undefined") | ||
| (function selfTest() { | ||
| const head = new ListNode(1, | ||
| new ListNode(2, | ||
| new ListNode(3, | ||
| new ListNode(4, | ||
| new ListNode(5) | ||
| ) | ||
| ) | ||
| ) | ||
| ); | ||
|
|
||
| console.log("Original list:"); | ||
| printList(head); | ||
| console.log("---"); | ||
|
|
||
| const reordered = oddEvenLinkedList(head); | ||
| console.log("Reordered (odd-even) list:"); | ||
| printList(reordered); | ||
| console.log("---"); | ||
| })(); |
This file contains hidden or 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,81 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| */ | ||
| export class ListNode { | ||
| constructor(value = 0, next = null) { | ||
| this.value = value; | ||
| this.next = next; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Rotates the linked list to the right by k places. | ||
| * @param {ListNode} head - The head of the linked list | ||
| * @param {number} k - Number of rotations | ||
| * @returns {ListNode} - The rotated linked list | ||
| */ | ||
| export function rotateRight(head, k) { | ||
| if (!head || k === 0) return head; | ||
|
|
||
| // Step 1: Find the length and tail | ||
| let length = 1; | ||
| let tail = head; | ||
| while (tail.next) { | ||
| tail = tail.next; | ||
| length++; | ||
| } | ||
|
|
||
| // Step 2: Make it circular | ||
| tail.next = head; | ||
|
|
||
| // Step 3: Find new tail position | ||
| const rotations = k % length; | ||
| const stepsToNewTail = length - rotations; | ||
| let newTail = head; | ||
| for (let i = 1; i < stepsToNewTail; i++) { | ||
| newTail = newTail.next; | ||
| } | ||
|
|
||
| // Step 4: Break the loop to form new head | ||
| const newHead = newTail.next; | ||
| newTail.next = null; | ||
|
|
||
| return newHead; | ||
| } | ||
|
|
||
| /** | ||
| * Helper function to print the linked list. | ||
| * @param {ListNode} head - The head of the linked list | ||
| */ | ||
| export function printList(head) { | ||
| const values = []; | ||
| let current = head; | ||
| while (current) { | ||
| values.push(current.value); | ||
| current = current.next; | ||
| } | ||
| console.log(values.join(" -> ")); | ||
| } | ||
|
|
||
| // Self-test for browser/console | ||
| if (typeof window !== "undefined") | ||
| (function selfTest() { | ||
| const head = new ListNode(1, | ||
| new ListNode(2, | ||
| new ListNode(3, | ||
| new ListNode(4, | ||
| new ListNode(5) | ||
| ) | ||
| ) | ||
| ) | ||
| ); | ||
|
|
||
| console.log("Original list:"); | ||
| printList(head); | ||
| console.log("---"); | ||
|
|
||
| const rotated = rotateRight(head, 2); | ||
| console.log("Rotated list by 2:"); | ||
| printList(rotated); | ||
| console.log("---"); | ||
| })(); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.