AfterAcademy Tech
•
06 Apr 2020

Difficulty: Medium
Asked in: Google, Amazon
Problem Description: You are given a linked list, write a program to remove the nth node from the end of the list and return the head.
Problem Note:
Example 1
Input: 2->6->1->9, n=2
Output: 2->6->9
Example 2
Input: 1->2->3->4, n=4
Output: 2->3->4
Example 3
Input: 4->9->1->2, n=1
Output: 4->9->1
The node structure given to you will be→
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x }
}
We will be discussing three different solutions
length- nth from the beginning.If we could save all the nodes in the array then, we could just remove the pointer of the (length-n-1)th node next to its next.next. In this way the nth node would be removed.
Pseudo Code
ListNode removeNthFromEnd(ListNode head, int n) {
ListNode list[]
ListNode curr = head
while(curr!=null){
list.append(curr)
curr = curr.next
}
int size = size(list)
if(size > 1){
if(size > n){
ListNode prev = list[size-n-1]
ListNode next = null
if(n > 1){
next = list[size-n+1]
}
prev.next = next;
} else {
head = list[1]
}
return head
}
return null
}
Complexity Analysis
Time Complexity — O(n)
Space Complexity — O(n)
Critical Ideas to Thnik
You can conclude from the previous approach that we don’t actually need to store the nodes in the array as we only need the length — nth node.
So, we could say that the problem is simply reduced to: Remove the (L−n+1) th node from the beginning in the list, where L is the list length.
Solution Step
(L−n)th node.next pointer of the (L−n)th node to the (L−n+2)th node.Pseudo Code
ListNode removeNthFromEnd(ListNode head, int n) {
ListNode curr = head
int end_index = 0
while(curr) {
end_index += 1
curr = curr->next;
}
index_to_remove = end_index - n
i = 0
curr = head
ListNode prev = nullptr
while(i < index_to_remove) {
prev = curr
curr = curr.next
i += 1
}
if(curr == head) {
head = head.next
}
else if(prev) {
prev.next = curr.next
}
delete curr
return head
}
Complexity Analysis
Time complexity: O(L) where L is the length of the linked list.
Space complexity: O(1)
Critical Ideas to Think
We could optimize the above approach where we only need a one-time traversal of the linked list. We could create two pointers, namely slow and a fast pointer pointing to the nodes that are n nodes apart from each other.
The fast pointer advances the list by n+1 steps from the beginning, while the slow pointer starts from the beginning of the list separating them by n nodes. We maintain this constant gap by advancing both pointers together until the fast pointer arrives past the last node. The slow pointer will be pointing at the nth node counting from the last. We relink the next pointer of the node referenced by the second pointer to point to the node’s next to the next node.
Solution steps
Pseudo Code
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode fast = head
ListNode dummy
dummy->next = head
ListNode slow = dummy
int step_ahead = 0
while(step_ahead < n-1){
fast = fast.next
step_ahead += 1
}
while(fast->next!=NULL){
slow = slow.next
fast = fast.next
}
//slow points to the node before target
slow->next = slow->next->next
return dummy->next
}
Complexity Analysis
Time complexity: O(L) where L is the length of the linked list.
Space complexity: O(1)
Critical Ideas to Think

If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
You are given the head of a linked list which probably contains a loop. If the list contains a loop, you need to find the last node of the list which points to one of its previous nodes to create a loop and make it point to NULL, thereby removing the loop.

AfterAcademy Tech
This is an interview problem asked by companies like Myntra, Visa, Oracle, Adobe, etc. The problem is to remove the duplicate nodes from the given Linked List so that only unique nodes(with unique data values) are present.

AfterAcademy Tech
This is an interview problem asked in companies like Amazon and Microsoft. Given a Linked List you have to swap nodes of the list in pairs.

AfterAcademy Tech
In this article we will explore the core structures of the Node.js architecture. Node.js is a popular framework for building backend systems (network applications) which can scale very well. Node.js is a Javascript runtime based on Chrome V8 engine.
