cartman
BAN USER
Comments (3)
Followers (3)
Reputation 60
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
2
of 2 vote
Take two pointers P1 and P2 and point both of them to the root of the linked list. Move P1 to Nth Node and then start moving both P1 and P2 until P1 reaches the end of linked list. In the end, P1 would be pointing to the Nth last node.
{{{
struct Node
{
Node * next;
int val;
}
Node *root; // Assuming the linked list is already made up.
int main()
{
cout<<"Enter the value of n";
cin>>n;
Node *p1 = root, *p2 = root;
for(int i = 0; p2 != NULL; )
{
if ( i > n )
{
p1 = p1->next;
}
p2 = p2->next;
i++;
} // for
if(i<n)
cout<< "Nodes insufficient!":
else
cout<<"Nth Last Node is"<<p1->val;
} // int main()
-
cartman
March 18, 2012 | Flag
Reply
Comment hidden because of low score. Click to expand.
Page:
1
Repmarktrejjo, Data Engineer at Accolite software
I’m Mark.I believe life is too short to be serious all the time, so if you cannot laugh ...
Repbarrybzane, AT&T Customer service email at ASU
By Profession, I am a Child protective services social worker in Newark USA. My strong interest is in yoga. My ...
Repjoycepwise, Blockchain Developer at Accenture
Hello, I’m Joyce. I’m a business developer living in Tampa, FL. I am a fan of music, travel ...
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
- cartman April 05, 2012