PuzzlersWorld.com

  • Hacker Puzzle
  • Interview Puzzles
  • Number Puzzles
  • Maths Puzzles

Find nth element from end of linked list

(No Ratings Yet)

November 4, 2012 by asinghal Leave a Comment

How to find nth element from the end of the linked list?

Or in other words Given an integer ‘n’, return a pointer to the n-th node from the end of the list.
Note: Linked list is singly linked list (there is only next pointer no previous pointer).

See Solution: find nth element from end of linked listHide Solution

Keep two pointers pointer1 and pointer2, both pointing to head of the linked list initially. Increment pointer1 by one node till it reaches (n+1)th node. Now increment both pointers by one node till pointer1 becomes null. At this point the node which pointer2 is pointing to will be the nth node from the last.

Code in C/C++

Node * getNthNodeFromLast(Node * node, int n){
	Node* pointer1 = node;
	Node* pointer2 = node;

	while(pointer1 != null && n > 0){
		pointer1 = pointer1->next;
		n--;
	}
	//List does not have n nodes, so return null
	if(n > 0)
		return null;
	//traverse till pointer1 becomes null
	while(pointer1 != null){
		pointer1 = pointer1->next;
		pointer2 = pointer2->next;
	}
	return pointer2;
}
  • Share on Whatsapp
  • Share on Facebook
  • Share on Twitter
Facebook Comments
Next Puzzle
Detect if there is a cycle in a linked list

Checkout more Interview Questions Tags: Amazon Interview Questions, Easy, Linked List, Solved Puzzles

Leave a Comment Cancel reply

Submit your Puzzle

You may also like

  • Osmos Solution: Google codejam 2013 Round 1B
  • Permutations solution: Facebook hacker cup 2013 Round 2
  • Lawnmower Solution: Google codejam 2013 Qual Round
  • Consonants Solution: Google codejam 2013 Round 1C
  • Reverse a link list without using another
  • Objective Questions set C/C++ – Part 2
  • Charging Chaos Solution – Google CodeJam
  • Subjective Question set C/C++ – Part 1
  • Basic Concepts C/C++
  • Cake Cutting solution: Facebook hacker cup 2013 Round 2

Categories

  • Aive hi Puzzles
  • Akbar and Birbal
  • Alphabetical Puzzles
  • Bollywood Puzzles
  • Google Code jam
  • Hindi Riddles
  • Interview Puzzles
  • Interview Questions
  • Logical Puzzles
  • Malayalam Puzzles
  • Maths Puzzles
  • Miscellaneous
  • Number Puzzles
  • Picture Puzzles
  • Riddles
  • Tamil Puzzles
  • Technical

Social

  • View puzzlersworld’s profile on Twitter
privacy policy

Copyright © 2023 · eleven40 Pro Theme on Genesis Framework · WordPress · Log in

  • Hacker Puzzle
  • Logo Puzzles
  • Optical Illusions
  • WhatsApp Puzzles
  • Picture Puzzles
  • Riddles
    ▼
    • Hindi Riddles
  • Bollywood Puzzles
  • Alphabetical Puzzles
  • Aive hi Puzzles
  • Interview Puzzles
  • Logical Puzzles
  • Interview Questions
    ▲
    • Data Structures
    • Binary Tree
    • Algorithms
    • Recursion Questions
    • Amazon Interview Questions
    • Snapdeal Interview Questions
    • Google Code jam
  • Technical
  • Akbar and Birbal
  • Number Puzzles
  • Maths Puzzles
  • Miscellaneous