Javascript Program For Inserting A Node After The N-th Node From The End
Last Updated :
25 Aug, 2022
Insert a node x after the nth node from the end in the given singly linked list. It is guaranteed that the list contains the nth node from the end. Also 1 <= n.
Examples:
Input : list: 1->3->4->5
n = 4, x = 2
Output : 1->2->3->4->5
4th node from the end is 1 and
insertion has been done after this node.
Input : list: 10->8->3->12->5->18
n = 2, x = 11
Output : 10->8->3->12->5->11->18
Method 1 (Using length of the list):
Find the length of the linked list, i.e, the number of nodes in the list. Let it be len. Now traverse the list from the 1st node upto the (len-n+1)th node from the beginning and insert the new node after this node. This method requires two traversals of the list.
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function getNode(data) {
var newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
function insertAfterNthNode(head , n , x) {
if (head == null )
return ;
var newNode = getNode(x);
var ptr = head;
var len = 0, i;
while (ptr != null ) {
len++;
ptr = ptr.next;
}
ptr = head;
for (i = 1; i <= (len - n); i++)
ptr = ptr.next;
newNode.next = ptr.next;
ptr.next = newNode;
}
function printList(head) {
while (head != null ) {
document.write(head.data + " " );
head = head.next;
}
}
var head = getNode(1);
head.next = getNode(3);
head.next.next = getNode(4);
head.next.next.next = getNode(5);
var n = 4, x = 2;
document.write( "Original Linked List: " );
printList(head);
insertAfterNthNode(head, n, x);
document.write();
document.write( "<br/>Linked List After Insertion: " );
printList(head);
</script>
|
Output:
Original Linked List: 1 3 4 5
Linked List After Insertion: 1 2 3 4 5
Time Complexity: O(n), where n is the number of nodes in the list.
Auxiliary space: O(1) because using constant variables
Method 2 (Single traversal):
This method uses two pointers, one is slow_ptr and the other is fast_ptr. First move the fast_ptr up to the nth node from the beginning. Make the slow_ptr point to the 1st node of the list. Now, simultaneously move both the pointers until fast_ptr points to the last node. At this point the slow_ptr will be pointing to the nth node from the end. Insert the new node after this node. This method requires single traversal of the list.
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function getNode(data) {
var newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
function insertAfterNthNode(head, n, x) {
if (head == null ) return ;
var newNode = getNode(x);
var slow_ptr = head;
var fast_ptr = head;
for ( var i = 1; i <= n - 1; i++)
fast_ptr = fast_ptr.next;
while (fast_ptr.next != null ) {
slow_ptr = slow_ptr.next;
fast_ptr = fast_ptr.next;
}
newNode.next = slow_ptr.next;
slow_ptr.next = newNode;
}
function printList(head) {
while (head != null ) {
document.write(head.data + " " );
head = head.next;
}
}
var head = getNode(1);
head.next = getNode(3);
head.next.next = getNode(4);
head.next.next.next = getNode(5);
var n = 4,
x = 2;
document.write( "Original Linked List: " );
printList(head);
insertAfterNthNode(head, n, x);
document.write( "<br>" );
document.write( "Linked List After Insertion:" );
printList(head);
</script>
|
Output:
Original Linked List: 1 3 4 5
Linked List After Insertion: 1 2 3 4 5
Time Complexity: O(n), where n is the number of nodes in the list.
Space Complexity: O(1) using constant space
Please refer complete article on Insert a node after the n-th node from the end for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...