diff --git a/dsa_in_js/linkedlist/odd_even_linked_list.js b/dsa_in_js/linkedlist/odd_even_linked_list.js new file mode 100644 index 0000000..ad25db3 --- /dev/null +++ b/dsa_in_js/linkedlist/odd_even_linked_list.js @@ -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("---"); + })(); diff --git a/dsa_in_js/linkedlist/rotate_linked_list.js b/dsa_in_js/linkedlist/rotate_linked_list.js new file mode 100644 index 0000000..eb4c643 --- /dev/null +++ b/dsa_in_js/linkedlist/rotate_linked_list.js @@ -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("---"); + })();