剑指offer-链表中环的入口点
来源:互联网 发布:苏州网络推广 编辑:程序博客网 时间:2024/06/05 03:20
题目:
一个链表中包含环,如何找出环的入口结点。
ListNode *MeetingNode(ListNode *head)//找出环中的一个结点,利用快慢指针来实现;{if (head == NULL)return NULL;ListNode *slow = head->next;ListNode *quick = head->next;if (slow == NULL)return NULL;while (quick != NULL&&slow != NULL){if (quick == slow)return quick;slow = slow->next;quick = quick->next;if (quick != NULL){quick = quick->next;}}return NULL;}ListNode *EntryNodeOfLoop(ListNode *pHead)//{ListNode *meetingNode = MeetingNode(pHead);ListNode *pNode1 = meetingNode;int numberOfLoop = 1;while (pNode1->next != meetingNode){pNode1 = pNode1->next;numberOfLoop++;}//求环中结点个数pNode1 = pHead;for (int i = 0; i < numberOfLoop; i++)pNode1 = pNode1->next;ListNode *pNode2 = pHead;while (pNode1 != pNode2){pNode1 = pNode1->next;pNode2 = pNode2->next;}return pNode1;}
阅读全文
0 0
- 剑指offer 56-链表中环的入口点
- 剑指offer-链表中环的入口点
- 剑指offer--链表中环的入口结点
- 《剑指offer》链表中环的入口结点
- 剑指offer—链表中环的入口结点
- 剑指offer:链表中环的入口结点
- [剑指offer]链表中环的入口结点
- 剑指Offer--056-链表中环的入口结点
- 剑指Offer: 链表中环的入口结点
- 剑指Offer:链表中环的入口节点
- 剑指offer-链表中环的入口结点
- 《剑指offer》-链表中环的入口结点
- 剑指Offer 56 链表中环的入口结点
- 剑指offer-链表中环的入口节点-php
- 剑指offer 链表中环的入口结点
- 剑指offer----链表中环的入口节点
- 7.剑指offer-链表中环的入口节点
- 剑指offer-55.链表中环的入口结点
- Ubuntu 安装openCV2.4.9 和cuda时 错误及对应解决方案
- Linux/CentOS Redis安装与开机启动配置
- js数组基本方法总结
- vim查找命令
- Oracle 静态SQL引起性能问题
- 剑指offer-链表中环的入口点
- 高桥和低桥(二分函数)
- jquery.Ajax soap方式调用webservice
- STS(Spring Tool Suite)使用前准备
- javascript复制变量值时,基本类型值和引用类型值异同
- C++析构函数为什么要为虚函数
- 身份证对应省份
- Sql server 忘记登录密码重置
- 降维与度量学习