Javascript Program For Rotating A Linked List (original) (raw)
Last Updated : 01 Apr, 2024
Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer. For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list should be modified to 50->60->10->20->30->40. Assume that k is smaller than the count of nodes in a linked list.
**Method 1:
To rotate the linked list, we need to change the next of kth node to NULL, the next of the last node to the previous head node, and finally, change the head to (k+1)th node. So we need to get hold of three nodes: kth node, (k+1)th node, and last node.
Traverse the list from the beginning and stop at kth node. Store pointer to kth node. We can get (k+1)th node using kthNode->next. Keep traversing till the end and store a pointer to the last node also. Finally, change pointers as stated above.
Below image shows how to rotate function works in the code :
JavaScript `
// Javascript program to rotate // a linked list
// Head of list let head;
// Linked list Node class Node { constructor(val) { this.data = val; this.next = null; } }
// This function rotates a linked list // counter-clockwise and updates the head. // The function assumes that k is smaller // than size of linked list. It doesn't // modify the list if k is greater than // or equal to size function rotate(k) { if (k == 0) return;
// Let us understand the below code
// for example k = 4 and list =
// 10->20->30->40->50->60.
let current = head;
// current will either point to kth or
// NULL after this loop. current will
// point to node 40 in the above example
let count = 1;
while (count < k && current != null) {
current = current.next;
count++;
}
// If current is NULL, k is greater than
// or equal to count of nodes in linked list.
// Don't change the list in this case
if (current == null)
return;
// current points to kth node. Store it in
// a letiable. kthNode points to node 40
// in the above example
let kthNode = current;
// current will point to last node after
// this loop current will point to node
// 60 in the above example
while (current.next != null)
current = current.next;
// Change next of last node to previous
// head Next of 60 is now changed to
// node 10
current.next = head;
// Change head to (k+1)th node
// head is now changed to node 50
head = kthNode.next;
// change next of kth node to null
kthNode.next = null;
}
/* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the list. */ function push(new_data) {
/* 1 & 2: Allocate the Node &
Put in the data */
let new_node = new Node(new_data);
// 3. Make next of new Node as head
new_node.next = head;
// 4. Move the head to point to new Node
head = new_node;
}
function printList() { let temp = head; while (temp != null) { console.log(temp.data + " "); temp = temp.next; } }
// Driver code // Create a list // 10->20->30->40->50->60 for (i = 60; i >= 10; i -= 10) push(i);
console.log("Given list
");
printList();
rotate(4);
console.log("Rotated Linked List
");
printList();
`
**Output:
Given linked list
10 20 30 40 50 60
Rotated Linked list
50 60 10 20 30 40
Time Complexity: O(n) where n is the number of nodes in Linked List. The code traverses the linked list only once.
**Method 2:
To rotate a linked list by k, we can first make the linked list circular and then moving k-1 steps forward from head node, making (k-1)th node's next to null and make kth node as head.
JavaScript `
`
**Output:
Given linked list
10 20 30 40 50 60
Rotated Linked list
50 60 10 20 30 40
Please refer complete article on Rotate a Linked List for more details!
Similar Reads
- Learn Data Structures with Javascript | DSA using JavaScript Tutorial JavaScript (JS) is the most popular lightweight, interpreted programming language, and might be your first preference for Client-side as well as Server-side developments. But have you thought about using JavaScript for DSA? Learning Data Structures and Algorithms can be difficult when combined with 7 min read
- Learn Algorithms with Javascript | DSA using JavaScript Tutorial This Algorithms with Javascript tutorial is designed to help you understand and implement fundamental algorithms using the versatile JavaScript programming language. Whether you are a beginner in programming or looking to enhance your algorithmic skills, this guide will walk you through essential co 15+ min read