crack the code interview 2.2

庾兴发
2023-12-01
//Implement an algorithm to find the nth to last element of a singly linked list.

struct LinkNode
{
    LinkNode * next;
    int value;
}
LinkNode * findLastN(LinkNode * head, int n)
{
    int i = 1;
    LinkNode * start;
    while (i < n && start != NULL)
    {
        start = start->next;
        i ++;
    }
    if (start == NULL)
        return NULL;
    LinkNode * h = head;
    while (start->next != NULL)
    {
        h = h->next;
        start = start->next;
    }
    return h;
}

 类似资料:

相关阅读

相关文章

相关问答