문제

I am trying to work on pointers. I am under a situation that i want to traverse the linked list after the second node(from third node).

I have the basic idea to traversing the linked list is:

while(temp!=NULL)
{
 temp=temp->next;
} 

So what i want is this linked list must start from third position. Could anyone please help me in preparing logic for that? I am not interested in any using any in-built functions. I will use this logic in other application.

도움이 되었습니까?

해결책

If your list is temp then temp is also the first element.

Then temp->next will be second element, and temp->next->next will be the third.

So if you write

temp = temp->next->next;

temp will be pointing to the third element and you can start your traverse from there.

다른 팁

Node *nth(Node *head, int nth){//nth : 0 origin
    while(nth && head){
        --nth;
        head = head->next;
    }
    return head;
}
...
Node *third = nth(head, 2);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top