Linked List Nth From End in JavaScript
Michael Mitrakos
5 min read
Efficiently insert a value at an offset from the end of a linked list in JavaScript with linear time complexity and constant space.
Linked List Nth From End in JavaScript
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital, I write about tech topics and teach engineers to have solid foundations that will help them get ahead in their career. I also build awesome products for digital nomads — check it out!
JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. Check out the ebook now!
In JavaScript, you can efficiently insert a value at an offset from the end of a linked list by following a linear time and constant space approach. Here’s an algorithm to accomplish this:
- Start by defining a function,
insertFromEnd, that takes the head of the linked list, the value to be inserted, and the offsetnas parameters. - Create two pointers,fastandslow, initially pointing to the head of the linked list. - Move thefastpointernnodes ahead in the linked list. Iffastreaches the end of the list before movingnnodes, return the original linked list unmodified. - Iffastreaches the end of the list exactly after movingnnodes, it means the offset is equal to the length of the list. Insert the new value at the head of the list and update the head pointer accordingly. - Iffastdoes not reach the end of the list, move bothfastandslowpointers simultaneously untilfastreaches the end of the list. - At this point, theslowpointer will bennodes away from the end of the list. Insert the new value after the node pointed to by theslowpointer. - Connect the new node with the subsequent nodes to maintain the integrity of the linked list. - Return the updated head of the linked list.
By following this algorithm, you can insert a value n spaces from the end of a linked list in linear time and constant space. This approach ensures efficient handling of the linked list while maintaining the constraints specified.
Code for Linked List Nth From End
Here’s an example illustrating the usage of the insertFromEnd function:
class ListNode {
this.next = null;
}
const newNode = new ListNode(value);
let fast = head;
let slow = head;
javascript return head; // Offset is greater than the length of the list
}
// Handle cases where the offset is 0 or equal to the length of the list
}
// Move both `fast` and `slow` pointers simultaneously
slow = slow.next;
// Insert the new node at the desired offset
newNode.next = slow.next;
slow.next = newNode;
}
// Example usage:
linkedList.next.next = new ListNode("C");
linkedList.next.next.next = new ListNode("D");
linkedList.next.next.next.next = new ListNode("E");
console.log("Linked List after Insertion: ", insertFromEnd(linkedList, "F", 2));In the given example, the original linked list is represented as A -> B -> C -> D -> E -> null. Calling `insertFromEnd(linkedList, "F", 2)` will insert the value "F" two spaces from the end, resulting in the modified linked list: A -> B -> C -> F -> D -> E -> null. The function handles different offset scenarios as specified in the explanation.
#### Conclusion
In conclusion, the “Linked List Nth From End” problem in JavaScript provides an interesting challenge when it comes to efficiently inserting a value at an offset from the end of a linked list. By implementing the `insertFromEnd` function with a linear time complexity and constant space complexity, we can achieve this task while adhering to the specified constraints.
The algorithm effectively utilizes two pointers, `fast` and `slow`, to traverse the linked list and identify the desired offset. It handles various scenarios such as when the offset is equal to the length of the list, when the offset is zero, and when the offset is greater than the length of the list. These considerations ensure the integrity and correctness of the resulting linked list.
By understanding and implementing this solution, we can efficiently manipulate linked lists and solve related problems in JavaScript. This problem serves as a great exercise for honing our skills in pointer manipulation and linked list traversal.
Overall, the “Linked List Nth From End” problem in JavaScript showcases the importance of efficient algorithms and data structure manipulation. Mastering such concepts not only enhances our problem-solving abilities but also equips us with valuable tools for building performant applications.
#### JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. [Check out the ebook now](https://www.mitrakos.com/ebook)!
I founded [Higglo Digital](https://higglo.io/) and we can help your business crush the web game with an award-winning website and cutting-edge digital strategy. If you want to see a *beautifully designed website*, [check us out](https://higglo.io/).
I also created [Wanderlust Extension](http://www.wanderlustapp.io/) to discover the most beautiful places across the world with highly curated content. Check it out!