crack the code interview 2.2

//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;
}

阅读更多
个人分类: crack the code interview
上一篇crack the code interview 2.1
下一篇crack the code interview 2.3
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭