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
76 changes: 76 additions & 0 deletions dsa_in_js/linkedlist/odd_even_linked_list.js
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("---");
})();
81 changes: 81 additions & 0 deletions dsa_in_js/linkedlist/rotate_linked_list.js
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("---");
})();