程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 面試題13:鏈表中倒數第k個結點

面試題13:鏈表中倒數第k個結點

編輯:C++入門知識

\


思路:設置兩個指針,一個慢指針pSlow,一個快指針pFast,快指針先走k-1步,接著兩個指針同時走,當快指針到達鏈表的尾部,慢指針恰好指向倒數第k個結點。

特殊情況:當k大於鏈表長度,則返回頭結點。

代碼:

 

#include "stdafx.h"   
#include <iostream>   
using namespace std;  
  
struct ListNode  
{  
    int m_nValue;  
    ListNode *m_pNext;  
};  
  
ListNode *FindKthToTail(ListNode *pHead, int k)  
{  
    if (pHead == NULL || k == 0)  
    {  
        return NULL;  
    }  
  
    ListNode *pSlow = pHead;  
    ListNode *pFast = pHead;  
    int i = 1;  
    while (i < k)  
    {  
       pFast = pFast->m_pNext;  
       if (pFast == NULL)  
       {  
           cout << "k = " << k << "大於鏈表長度!" << endl;  
           break;  
       }  
       i++;  
    }  
  
    if (pFast != NULL)  
    {  
        while (pFast->m_pNext != NULL)  
        {  
            pSlow = pSlow->m_pNext;  
            pFast = pFast->m_pNext;  
        }  
    }  
    return pSlow;  
}  
  
//創建一個鏈表,輸入從頭到尾結點的值,輸入-1表示結束   
void CreateList(ListNode *& pHead)  
{     
    ListNode *pListNode = NULL;  
    ListNode *pCurLastNode = NULL;  
    bool isHead = true;  
  
    while (1)  
    {  
        if (isHead)  
        {     
            pHead = new ListNode();  
            cin >> pHead->m_nValue;  
            pHead->m_pNext = NULL;  
            isHead = false;  
            pCurLastNode = pHead;  
        }  
        else  
        {  
            pListNode = new ListNode();  
            cin >> pListNode->m_nValue;  
            if (pListNode->m_nValue == -1)  
            {  
                break;  
            }  
            pListNode->m_pNext = NULL;  
            pCurLastNode->m_pNext = pListNode;  
            pCurLastNode = pListNode;     
        }             
    }  
}  
  
//從頭到尾打印鏈表   
void PrintList(ListNode *&pHead)  
{  
    if (pHead != NULL)  
    {  
        ListNode *pCur = pHead;  
        while (pCur != NULL)  
        {  
            cout << pCur->m_nValue << " ";  
            pCur = pCur->m_pNext;  
        }  
        cout << endl;  
    }  
    else  
    {  
        cout << "鏈表為空!" << endl;  
    }  
}  
  
int _tmain(int argc, _TCHAR* argv[])  
{  
    ListNode *pListHead = NULL;  
    CreateList(pListHead);  
    PrintList(pListHead);  
    ListNode *pKthNodeToTail = FindKthToTail(pListHead, 6);  
    cout << pKthNodeToTail->m_nValue << endl;  
    return 0;  
}  
#include "stdafx.h"
#include <iostream>
using namespace std;

struct ListNode
{
	int m_nValue;
	ListNode *m_pNext;
};

ListNode *FindKthToTail(ListNode *pHead, int k)
{
	if (pHead == NULL || k == 0)
	{
		return NULL;
	}

	ListNode *pSlow = pHead;
	ListNode *pFast = pHead;
	int i = 1;
	while (i < k)
	{
       pFast = pFast->m_pNext;
	   if (pFast == NULL)
	   {
		   cout << "k = " << k << "大於鏈表長度!" << endl;
		   break;
	   }
	   i++;
	}

	if (pFast != NULL)
	{
		while (pFast->m_pNext != NULL)
		{
			pSlow = pSlow->m_pNext;
			pFast = pFast->m_pNext;
		}
	}
	return pSlow;
}

//創建一個鏈表,輸入從頭到尾結點的值,輸入-1表示結束
void CreateList(ListNode *& pHead)
{	
	ListNode *pListNode = NULL;
	ListNode *pCurLastNode = NULL;
	bool isHead = true;

	while (1)
	{
		if (isHead)
		{	
			pHead = new ListNode();
			cin >> pHead->m_nValue;
			pHead->m_pNext = NULL;
			isHead = false;
			pCurLastNode = pHead;
		}
		else
		{
			pListNode = new ListNode();
			cin >> pListNode->m_nValue;
			if (pListNode->m_nValue == -1)
			{
				break;
			}
			pListNode->m_pNext = NULL;
			pCurLastNode->m_pNext = pListNode;
			pCurLastNode = pListNode;	
		}			
	}
}

//從頭到尾打印鏈表
void PrintList(ListNode *&pHead)
{
	if (pHead != NULL)
	{
		ListNode *pCur = pHead;
		while (pCur != NULL)
		{
			cout << pCur->m_nValue << " ";
			pCur = pCur->m_pNext;
		}
		cout << endl;
	}
	else
	{
		cout << "鏈表為空!" << endl;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	ListNode *pListHead = NULL;
	CreateList(pListHead);
	PrintList(pListHead);
	ListNode *pKthNodeToTail = FindKthToTail(pListHead, 6);
	cout << pKthNodeToTail->m_nValue << endl;
	return 0;
}



 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved