【leetcode】【单链表】【142】Linked List Cycle II
来源:互联网 发布:乐乎lofte网页版 编辑:程序博客网 时间:2024/05/16 18:14
#include<iostream>using namespace std;struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};class Solution {public:ListNode *detectCycle(ListNode *head) {ListNode* slow = head;ListNode* fast = head;while (fast&&fast->next){slow = slow->next;fast = fast->next->next;if (slow == fast){ListNode* cur = head;while (cur != slow){cur = cur->next;slow = slow->next;}return cur;}}return NULL;}ListNode* createList(ListNode* head){int numOfNode;int value;cout << "please input number of listNode:";cin >> numOfNode;cin >> value;head = new ListNode(value);ListNode* cur = head;for (int i = 1; i < numOfNode; ++i){cin >> value;ListNode* temp = new ListNode(value);cur->next = temp;cur = temp;}cur->next = head->next;return head;}void printNode(ListNode* head){ListNode* cur = head;while (cur){cout << cur->val << " ";cur = cur->next;}cout << endl;}};int main(){ListNode* head = NULL;Solution solution;head = solution.createList(head);//solution.printNode(head);cout << solution.detectCycle(head)->val << endl;system("pause");return 0;}
参考:http://blog.sina.com.cn/s/blog_725dd1010100tqwp.html
0 0
- 【leetcode】【单链表】【142】Linked List Cycle II
- LeetCode.142(141) Linked List Cycle && Linked List Cycle (II)
- LeetCode 142 Linked List Cycle II
- LeetCode 142 Linked List Cycle II
- LeetCode: Linked List Cycle II [142]
- [leetcode 142]Linked List Cycle II
- Leetcode NO.142 Linked List Cycle II
- [leetcode]142 Linked List Cycle II
- [LeetCode 142]Linked List Cycle II
- leetcode || 142、Linked List Cycle II
- [Leetcode 142] Linked List Cycle II
- [leetcode-142]Linked List Cycle II(c)
- leetcode 142: Linked List Cycle II
- leetCode #142 Linked List Cycle II
- Leetcode #142 Linked List Cycle II
- 142 Linked List Cycle II [Leetcode]
- 【LEETCODE】142-Linked List Cycle II
- LeetCode 142 Linked List Cycle II
- 配python环境——intelliJ IDEA & PostgreSQL
- 浩易南:如何年赚50万?
- 哈希的原理和代价
- 欢迎使用CSDN-markdown编辑器
- redis学习笔记2
- 【leetcode】【单链表】【142】Linked List Cycle II
- HASH表原理
- linux下使用SWIG将C/C++库移植到Java
- require.js 入门学习 (share)
- 白话算法之【动态规划入门】
- 在Activity中使用Thread导致的内存泄漏
- AppContextUtils
- 虚拟机克隆Centos后的网卡配置
- hash算法 (hashmap 实现原理)