首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
输入一个单向链表,输出该链表中倒数第k个结点。链表的倒数第0个结点为链表的尾指针。链表结点定义如下: struct ListNode { int m_nKey; ListNode* m_pNext; };
输入一个单向链表,输出该链表中倒数第k个结点。链表的倒数第0个结点为链表的尾指针。链表结点定义如下: struct ListNode { int m_nKey; ListNode* m_pNext; };
admin
2019-03-29
108
问题
输入一个单向链表,输出该链表中倒数第k个结点。链表的倒数第0个结点为链表的尾指针。链表结点定义如下:
struct ListNode
{
int m_nKey;
ListNode* m_pNext;
};
选项
答案
/////////////////////////////////////////////////////////////////////// // Find the kth node from the tail of a list // Input: pListHead - the head of list // k - the distance to the tail // Output: the kth node from the tail of a list /////////////////////////////////////////////////////////////////////// ListNode* FindKthToTail_Solution1(ListNode* pListHead, unsigned int k) { if(pListHead == NULL) return NULL; // count the nodes number in the list ListNode *pCur = pListHead; unsigned int nNum = 0; while(pCur->m_pNext != NULL) { pCur = pCur->m_pNext; nNum ++; } // if the number of nodes in the list is less than k // do nothing if(nNum < k) return NULL; // the kth node from the tail of a list // is the (n - k)th node from the head pCur = pListHead; for(unsigned int i = 0; i < nNum - k; ++ i) pCur = pCur->m_pNext; return pCur; } 思路二的参考代码: /////////////////////////////////////////////////////////////////////// // Find the kth node from the tail of a list // Input: pListHead - the head of list // k - the distance to the tail // Output: the kth node from the tail of a list /////////////////////////////////////////////////////////////////////// ListNode* FindKthToTail_Solution2(ListNode* pListHead, unsigned int k) { if(pListHead == NULL) return NULL; ListNode *pAhead = pListHead; ListNode *pBehind = NULL; for(unsigned int i = 0; i < k; ++ i) { if(pAhead->m_pNext != NULL) pAhead = pAhead->m_pNext; else { // if the number of nodes in the list is less than k, // do nothing return NULL; } } pBehind = pListHead; // the distance between pAhead and pBehind is k // when pAhead arrives at the tail, p // Behind is at the kth node from the tail while(pAhead->m_pNext != NULL) { pAhead = pAhead->m_pNext; pBehind = pBehind->m_pNext; } return pBehind; }
解析
为了得到倒数第k个结点,很自然的想法是先走到链表的尾端,再从尾端回溯k步。可是输入的是单向链表,只有从前往后的指针而没有从后往前的指针。因此我们需要打开我们的思路。
既然不能从尾结点开始遍历这个链表,我们还是把思路回到头结点上来。假设整个链表有n个结点,那么倒数第k个结点是从头结点开始的第n-k-1个结点(从0开始计数)。如果我们能够得到链表中结点的个数n,那我们只要从头结点开始往后走n-k-1步就可以了。如何得到结点数n?这个不难,只需要从头开始遍历链表,每经过一个结点,计数器加一就行了。
这种思路的时间复杂度是O(n),但需要遍历链表两次。第一次得到链表中结点个数n,第二次得到从头结点开始的第n?-k-1个结点即倒数第k个结点。
如果链表的结点数不多,这是一种很好的方法。但如果输入的链表的结点个数很多,有可能不能一次性把整个链表都从硬盘读入物理内存,那么遍历两遍意味着一个结点需要两次从硬盘读入到物理内存。我们知道把数据从硬盘读入到内存是非常耗时间的操作。我们能不能把链表遍历的次数减少到1?如果可以,将能有效地提高代码执行的时间效率。
如果我们在遍历时维持两个指针,第一个指针从链表的头指针开始遍历,在第k-1步之前,第二个指针保持不动;在第k-1步开始,第二个指针也开始从链表的头指针开始遍历。由于两个指针的距离保持在k-1,当第一个(走在前面的)指针到达链表的尾结点时,第二个指针(走在后面的)指针正好是倒数第k个结点。
这种思路只需要遍历链表一次。对于很长的链表,只需要把每个结点从硬盘导入到内存一次。因此这一方法的时间效率前面的方法要高。
转载请注明原文地址:https://kaotiyun.com/show/4RmZ777K
0
程序员面试
相关试题推荐
Thecommitteehasanticipatedtheproblemsthat______intheroadconstructionproject.
YourfriendDavidjustsentyouabookthatyouexpectedasabirthdaypresent.Sendane-mailtohimtoexpressyourappreciati
AWelcomeE-mail欢迎邮件Someinternationalstudentsarecomingtoyouruniversity.Writethemane-mailinthenameoftheStudent
Weakdollarorno,$46,000—thepriceforasingleyearofundergraduateinstructionamidtheredbrickofHarvardYard—is【C1】__
大概描述一下ASP。NET页面的生命周期
活动目录的作用
bob的电子邮件转发到wanglong@sina.com。
计算机能直接识别和执行的语言是()A.机器语言B.高级语言C.数据库语言D.汇编程序
关于计算机语言的描述,不正确的是()。A.机器语言的语句全部由0和1组成,指令代码短,执行速度快B.机器语言因为是面向机器的低级语言,所以执行速度慢C.汇编语言已将机器语言符号化,所以它与机器无关D.汇编语言比机器语言执行速度快
随机试题
组织等节奏流水施工的前提条件是()。
高速机械试运转时,如从低速到高速无异常现象,可直接进行满负荷试运转。( )
Thefamilylookedonhelplesslyastheirhouse______.
国家卫生健康委员会医政医管局有关负责人表示,2017年我国门诊抗菌药物使用率下降到7.7%,住院患者抗菌药物使用率下降到36.8%。我国细菌耐药趋势总体平稳,临床合理用药水平不断提升。卫生健康部门还将推动建立儿童医院门急诊和住院抗菌药物使用监控制度,加强
下列阐述城市生态系统的人为性有误的是()。
电算化会计数据处理包括四个基本环节,其中()是中心环节。
卖方承担责任最大的是()。
群体极化指的是群体成员中存在的某一种倾向性,通过群体相互影响而使结果得到加强并趋于极端化,从而偏离某种合理性的现象。群体极化通常存在两种情形,一种是使结果变得更为冒险甚至激进,称为冒险偏移;一种是使结果变得更加保守,称为谨慎偏移。根据上述定义,下列哪项中的
()是构建社会主义和谐社会的最根本保证。
"Everythinghappensforthebest",mymothersaid(31)Ifaceddisappointment."Ifyoucarryon,onedaysomethinggoodwill(32
最新回复
(
0
)