Implement a function that returns the 5th element from the
end in a singly linked list of integers in one pass.
Answer Posted / abhijit annaldas
Sorry, it was my mistake.. previous answer was not correct.
Here is the corrected one...
node* getNthFromLast(node* head, int n)
{
int c=0;
node *nth=head;
node *pt=head;
while(pt!=NULL)
{
pt=pt->next;
c++;
//if c=n then nth node is already set to head.
if(c>n)
nth=nth->next;
}
if(c<n) //LL contains less than n nodes
return (*node)0;
else
return *nth;
}
Use it as..
fifth_node = getNthFromLast(head, 5);
| Is This Answer Correct ? | 3 Yes | 0 No |
Post New Answer View All Answers
any "C" function by default returns an a) int value b) float value c) char value d) a & b
Describe the difference between = and == symbols in c programming?
Why structure is used in c?
Explain about the constants which help in debugging?
Explain a pre-processor and its advantages.
Program will then find the largest of three numbers using nested if-else statements. User is prompted to enter three numbers. Program will find the largest number and display it on the screen. All three numbers entered by the user are also displayed. If user enters 21, 33, and 5, the output should be as follows: You entered: 21, 33 and 5. The largest number is 33.
Is c easy to learn?
Explain pointer. What are function pointers in C?
What extern c means?
Is it possible to have a function as a parameter in another function?
When should the volatile modifier be used?
how to capitalise first letter of each word in a given string?
What are the complete rules for header file searching?
What is the use of static variable in c?
What is an auto variable in c?