Węzeł n -ten od końca listy powiązanej
/* struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};
*/
//Function to find the data of nth node from the end of a linked list.
int getNthFromLast(Node *head, int n)
{
// Your code here
Node* current, * prev, * temp;
current = head;
prev = NULL;
while (current != NULL)
{
temp = current->next;
current->next = prev;
prev = current;
current = temp;
}
head = prev;
int cnt=1;
while(head!=NULL)
{
if(cnt==n&&head!=NULL)
{
return head->data;
}
cnt++;
head=head->next;
}
return -1;
}
coder