This post is completed by 1 user
|
Add to List |
103. Delete X Nodes After Y Nodes In a Linked List
Objective: Given a Linked List and x and y. Delete x number of nodes after y nodes from the start.
Example:
-10-20-30-40-50-60-70-80-90-100-110-120 Deleted 4 Nodes after 5 Nodes -10-20-30-40-50-100-110-120
Approach:
- We need two pointers.
- One pointer at one node prior to the nodes to be deleted. ( Move it by y starting from the head).
- Another pointer at one node after to the nodes to be deleted. (Move it by x starting from the y, as per the previous step).
- Then Just link these two nodes.
Code:
Output:
-10-20-30-40-50-60-70-80-90-100-110-120 Deleted 4 Nodes after 5 Nodes -10-20-30-40-50-100-110-120