代码改变世界

面试题9:单链表中倒数第k个结点

2016-03-29 21:53  Keiven_LY  阅读(658)  评论(0编辑  收藏  举报

题目描述:

输入一个单链表,输出该链表中倒数第k个结点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾结点为倒数第1个结点。如一个链表有6个结点,从头到尾依次为:1、2、3、4、5、6,则这个链表的倒数第2个结点是值为5的结点。

思路1首先求出单链表的总长度n,然后从链表的头节点开始遍历,当遍历到n-k+1个节点时,即为链表中倒数第k个节点

思路2首先设置两个指针p1和p2,让p1先向前走k-1步,p2保持不动;从第k步开始,p2页开始从头结点开始遍历。由于p1和p2的距离始终保持在k-1,当p1走到链表的尾结点时,p2正好是倒数第k个结点。

思路1的功能函数:

/*     思路1:链表中倒数第k个结点    */
Node *findKthToTail1(Node *head, int k)
{
    int num = 0;
    Node *p = head;
    while(p != NULL)
    {
        num++;
        p = p->next;
    }
    if(num == 0 || k > num)
        return NULL;

    p = head;

    for( int i = 0; i < num-k; i++)
    {
        p = p->next ;
    }
    return p;
}

思路2的功能函数:

/*     思路2:链表中倒数第k个结点    */
Node *findKthToTail2(Node *head, int k)
{ 
    int num = 0;
    Node *p = head;
    Node *p1, *p2;
    while(p != NULL)
    {
        num++;
        p = p->next;
    }
    if(num == 0 || k > num)
        return NULL;

    p1 = p2 = head; //开始p1和p2都指向头结点
    for(int i = 0; i< k; i++)
    {
        p1 = p1->next;
    }
    while(p1 != NULL)
    {
        p1 = p1->next;
        p2 = p2->next;
    }
    return p2;
}

完整可执行程序:

#include<iostream>
#include <stack>
#include<stdlib.h> 
#include<time.h>

using namespace std;

typedef struct node
{
    int data;
    struct node *next;
}Node;

/*     创建含有n个结点的单链表    */
Node *CreateListHead(int n) 
{
    Node *head;
    head=(Node *)malloc(sizeof(Node)); /*创建头结点*/
    Node *q = head;

    /* 初始化随机数种子 */
    srand(time(0));  //srand函数在stdlib.h头文件中,time函数在time.h头文件中

    for(int i=0; i < n; i++)
    {
        Node *p = (Node *)malloc(sizeof(Node));
        p->data = rand()%100+1;  //随机生成100以内的数字 
        p->next = q->next;
        q->next = p;
        q = p;
    }
    q->next = NULL;

    return head;
}

/****打印单链表******/
void print(Node *head)
{
    Node *p;
    if(head->next==NULL)
    {
        cout << "The LinkList is Empty !" <<endl;
        return;
    }
    p=head->next;
    while(p!=NULL)
    {
        cout << p->data << " " ;
        p=p->next;
    }
}
/*     思路1:链表中倒数第k个结点    */
Node *findKthToTail1(Node *head, int k)
{
    int num = 0;
    Node *p = head;
    while(p != NULL)
    {
        num++;
        p = p->next;
    }
    if(num == 0 || k > num)
        return NULL;

    p = head;

    for( int i = 0; i < num-k; i++)
    {
        p = p->next ;
    }
    return p;
}

/*     思路2:链表中倒数第k个结点    */
Node *findKthToTail2(Node *head, int k)
{ 
    int num = 0;
    Node *p = head;
    Node *p1, *p2;
    while(p != NULL)
    {
        num++;
        p = p->next;
    }
    if(num == 0 || k > num)
        return NULL;

    p1 = p2 = head; //开始p1和p2都指向头结点
    for(int i = 0; i< k; i++)
    {
        p1 = p1->next;
    }
    while(p1 != NULL)
    {
        p1 = p1->next;
        p2 = p2->next;
    }
    return p2;
}
int main()
{
    Node *SingleLinkList = NULL;
    int length;

    cout << "Please input the length of LinkList: ";
    cin >> length;
    SingleLinkList = CreateListHead(length);
    cout << "The new created LinkList as below: " ;
    print(SingleLinkList);
    cout << endl;

    Node *KthNode = NULL;
    int kth;
    cout << "思路1:请输入要查找的倒数第几个节点:  " ;
    cin >> kth;
    KthNode = findKthToTail1(SingleLinkList, kth);
    cout << "链表倒数第" << kth << "个结点为:" << KthNode->data <<endl;

    cout << "思路2:请输入要查找的倒数第几个节点:  " ;
    cin >> kth;
    KthNode = findKthToTail2(SingleLinkList, kth);
    cout << "链表倒数第" << kth << "个结点为:" << KthNode->data <<endl;

    system("pause");
    return 0;
}

运行结果: