Find the modular node of a linked list from the end. If possible, do it in a single pass?
I think there is some mistake in this question means from last n%k==0 is 17th Node not 16th Node.
Check this: 1->2->3->4->5->6->7->8->9->10->11->12->13->14->15->16->17->18->19
Start counting: 19 is at 1st position from end. So 1%3 !=0
18 is at 2nd position from end. So 2%3 !=0
17 is at 3rd position from end. So 3%3 ==0.
17 is a n%k==0 Node from end.
Yes, we can find this in a single pass. For this take two reference one is head and other is mod_Node. First move your head reference till k position after that start moving your mod_Node with head once your head reached at null then at that time your mod_Node pointing to "n%k==0 from end" Node.
public Node modNode(Node head,int k)
{
Node mod_Node=head;
int i=0;
while(head!=null)
{
if(i<k)
i++;
else
mod_Node=mod_Node.next;
head=head.next;
}
return mod_Node;
}
The Question is just a different way of asking for the kth element from the end of the linked list. Since the first element from the end for which we have n%k=0 , has to be the kth element (considering 1-based indexing of nodes).
Suppose total number of nodes are N(we don't know N). So kth from end means (N-k+1)th from beginning.
Now for doing it in single pass, we take two pointer approach.
Suppose we have two pointers fast and slow.
Initially both points to head node of the list.
slow starts moving only after fast has made k-1 moves i.e slow moves only after fast points to the kth element from the beginning.
From there both move forward until fast reaches the end of the list.
NOTE: At any point of time both the pointers move one node at a time.
What this means is once fast is at the kth node from beginning, it has (N-k) moves to reach the last node. Which means the slow pointer will too be moving (N-k) moves from there and will reach the (N-k+1)th node.
A bit of intuition: To reach the kth node from beginning we need (k-1)moves, so to reach
(N-k+1)th node from beginning we need (N-k+1-1)=(N-k) moves. And after moving fast to kth node we are left with (N-k) moves.
Now the code:
//Our structure:
struct Node{
int data;
Node* next;
};
//Function to return the kth node from end.
Node* kthNodeFromEnd(Node* head,int k){
Node *fast,*slow;
int l=1;
fast=slow=head;
while(fast->next!=NULL){
fast=fast->next;
l++;
// Move slow only after fast has reached kth node.
// From there both moves together.
if(l>k){
slow=slow->next;
}
}
return slow;
}
Question Clarification:
Now for n=19 and k=3, if we number the nodes from the beginning using 1-based index we will get 17th node as our answer, but if we number the nodes using 0-based index, we will get 16th node as the answer. May be the source of your question had 0-based indexing of nodes.
Note: "n" is defined as the number of nodes in the list, we can number the nodes in any way. Say for n=1 , we have 0th node.For n=2, we have 0th,1st node and so on with us.