Nth node from end of linked list | Practice | GeeksforGeeks

Given a linked list consisting of L nodes and given a number N. The task is to find the Nth node from the end of the linked list.
Example 1:
Input:
N = 2
LinkedList: 1->2->3->4->5->6->7->8->9
Output: 8
Explanation: In the first example, there
are 9 nodes in linked list and we need
to find 2nd node from end. 2nd node
from end os 8.  

Example 2:
Input:
N = 5
LinkedList: 10->5->100->5
Output: -1
Explanation: In the second example, there
are 4 nodes in the linked list and we
need to find 5th from the end. Since 'n'
is more than the number of nodes in the
linked list, the output is -1.
Your Task:
The task is to complete the function getNthFromLast() which takes two arguments: reference to head and N and you need to return Nth from the end or -1 in case node doesn't exist..
Note:
Try to solve in single traversal.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 <= L <= 106
1 <= N <= 106
0
2019sushilkumarkori9 hours ago
int getNthFromLast(Node *head, int n)
{
       // Your code here
       int l=0;
       Node* temp = head;
       while(temp!=NULL){
           l++;
           temp=temp->next;
       }
       if(n>l){
           return -1;
       }
       temp=head;
       int i=0;
       while(temp!=NULL){
           i++;
           if(i == (l-n)+1){
               return temp->data;
           }
           temp=temp->next;
       }
       return -1;
}
0
jimmynandwana5 days ago
int getNthFromLast(Node head, int n)   {    // Your code here     Node low = head;    Node high = head;    for(int i = 1 ; i<n;i++){        if(high.next != null){        high = high.next;       }       else{           return -1;       }    }    while(high != null && high.next != null){        low = low.next;        high = high.next;    }    return low.data;   }
+1
bipulharsh1235 days ago
Time : O(N)
Space: O(1)
 
int getNthFromLast(Node *head, int n){      // Your code here      Node *ptr=NULL;      Node *startBatch=head;      int count=0;      while(head){          count = (count+1)%n;          if(count%n==0){              ptr = startBatch;           startBatch = head->next;          }          head = head->next;      }            if(!ptr)       return -1;      while(count--){          ptr = ptr->next;      }       return ptr->data;}
0
sailendrad1 week ago
   int getNthFromLast(Node head, int n)   {    // Your code here    Node fast = head;    Node slow = head;            while(n > 1){        fast = fast.next;        if(fast == null)return -1;                n--;    }        while(fast.next !=null){        fast= fast.next;        slow= slow.next;    }      return slow.data;    }
0
sudeepgarg6711 week ago
int getNthFromLast(Node *head, int n){      Node *temp = head;      int count = 0;      while(temp!=NULL){          count++;          temp = temp->next;      }      if(n>count){          return -1;      }      int start = 0;      Node *temp2 = head;      while(start!=count-n){          temp2 = temp2->next;          start++;      }      return temp2->data;}
0
nischithtn1 week ago
 
 
int getNthFromLast(Node head, int n)   {    // Your code here     LinkedList<Integer> l = new LinkedList<Integer>();    while(head!=null){                l.add(head.data);        head=head.next;        }    int p=l.size();    int k =p-n;    int z=-1;       if(n<=p){         z=l.get(k);    }else{        z=-1;    }    return z;   }
0
akaashhardha1 week ago
int getNthFromLast(struct Node *head, int n){   int count = 0;      struct Node *temp = head;      while(temp != NULL){       count++;       temp = temp->next;   }      struct Node *temp2 = head;   if(n <= count){       for(int i = 1; i <= count - n ; i++)           temp2 = temp2->next;        return temp2->data;   }   else       return -1;}
 
+2
nimox1 week ago
int getNthFromLast(Node head, int n)
    {
    	// Your code here	
    	Node temp = head;
    	Node ans = head;
    	int count = 0;
    	
    	while(temp!=null)
    	{
    	    if(count>=n)    ans=ans.next;
    	    
    	    temp = temp.next;
    	    count++;
    	}
    	
    	if(n>count)   return -1;
    	return ans.data;
    }
+1
shahabuddinbravo401 week ago
int getNthFromLast(Node *head, int n){      Node *ptr=head;      int count=0;      while(ptr!=NULL)      {          ptr=ptr->next;          count++;      }            if(count-n >= 0){          Node *ptr1=head;          int index=1;          while(ptr1!=NULL){              if(index==(count-n)+1){                  return ptr1->data;              }              index++;              ptr1=ptr1->next;          }      }      else{          return -1;      }}
 
0
vaibhav9812031 week ago
JAVA 
 
    int getNthFromLast(Node head, int n)   {    // Your code here    int count  = 1;    Node n1 = head;    while(n1.next!=null){        n1 = n1.next;        count++;    }    int newIndex = count-n;    if(count == n)        return head.data;    if(n > count)        return -1;    n1 = head;    count = 1;    while(n1.next!=null){        if(count == newIndex)            break;        n1 = n1.next;        count++;
    }    Node result = n1.next;    return result.data;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.