LintCode-带环链表 II
来源:互联网 发布:美工岗位描述 编辑:程序博客网 时间:2024/05/16 11:18
给定一个链表,如果链表中存在环,则返回到链表中环的起始节点的值,如果没有环,返回null。
您在真实的面试中是否遇到过这个题?Yes
样例
给出 -21->10->4->5, tail connects to node index 1,返回10
挑战
标签 Expand 不使用额外的空间
相关题目 Expand
分析:首先肯定可以利用快慢指针来判断该链表中有没有环,如果快慢指针能够遇到,那么肯定存在环,那么完全可以把环打破,变成求两个链表的第一个交叉点问题,也是有O(n)且不消耗空间的解法的。
代码:
/** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next = NULL; * } * } */class Solution {public: /** * @param head: The first node of linked list. * @return: The node where the cycle begins. * if there is no cycle, return null */ ListNode *detectCycle(ListNode *head) { // write your code here if(head == nullptr) return nullptr; ListNode*fast = head; ListNode*slow = head; while(true) { if(fast&&fast->next) { fast = fast->next->next; slow = slow->next; if(fast==slow) { ListNode*pre = findPreNode(slow); pre->next = nullptr; ListNode*ret = findFirstIntersect(head,slow); return ret; } } else { return nullptr; } } } ListNode*findPreNode(ListNode*node) { ListNode*temp = node; while(temp->next!=node) temp = temp->next; return temp; } ListNode*findFirstIntersect(ListNode*head1,ListNode*head2) { int len1 = 0; ListNode*temp1 = head1; while(temp1){len1++;temp1=temp1->next;} int len2 = 0; ListNode*temp2 = head2; while(temp2){len2++;temp2=temp2->next;} if(len1>len2) { int k = len1-len2; while(k--) head1 = head1->next; } else { int k = len2-len1; while(k--) head2 = head2->next; } while(head1!=head2) { head1 = head1->next; head2 = head2->next; } return head1; }};
0 0
- LintCode-带环链表 II
- LintCode: 带环链表 II
- *[Lintcode]带环链表 II
- 带环链表 II-LintCode
- lintcode--带环链表II
- LintCode(103)带环链表 II
- lintcode(103)带环链表 II
- [LintCode] 带环链表 II Linked List Cycle II
- lintcode-带环链表
- lintcode,带环链表
- 带环链表-LintCode
- [Lintcode]带环链表
- lintcode带环链表
- LintCode 带环链表
- lintcode-带环链表-102
- lintcode(102)带环链表
- [LintCode]102.带环链表
- LintCode:M-带环链表
- Android studio 快捷键分享(Mac osx 10.5+)
- 本地通知
- Maven内置隐式变量
- Bjarne Stroustrup 语录
- MINIGUI学习总结一
- LintCode-带环链表 II
- NOIP2014联合权值——史上最强解析
- C++基础知识之struct和class的区别
- 前端优化总结
- 我的ROS入门(三):ubuntu-15.04-desktop-amd64配置ROS
- 浅谈多进程多线程的选择
- HDU 1978
- Subsets
- UVA 340 Master-Mind Hints