4.24 leetcode -24 linked-list-cycle
来源:互联网 发布:网络调研公司排名 编辑:程序博客网 时间:2024/06/05 02:02
题目描述
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
给一个链表,看是否有循环,不用额外控件,一个一步走,一个两步走,能汇合就有cycle
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool hasCycle(ListNode *head) { ListNode * ponestep = head; ListNode * ptwostep = head; if(head == NULL) return false; else if(head->next == NULL) return false; ptwostep = ptwostep->next; while(ptwostep!= ponestep && ptwostep->next != NULL && ptwostep->next->next !=NULL) { ptwostep =ptwostep->next->next; ponestep = ponestep->next; } if(ptwostep== ponestep) return true; else return false; }};
阅读全文
0 0
- 4.24 leetcode -24 linked-list-cycle
- leetcode Linked List Cycle & Linked List Cycle ||
- 【LeetCode】Linked List Cycle
- Leetcode: Linked List Cycle
- Leetcode Linked List Cycle
- Leetcode: Linked List Cycle
- LeetCode:Linked List Cycle
- [LeetCode]Linked List Cycle
- LeetCode | Linked List Cycle
- Leetcode: Linked List Cycle
- LeetCode: Linked List Cycle
- LeetCode - Linked List Cycle
- [LeetCode] - Linked List Cycle
- LeetCode - Linked List Cycle
- [LeetCode]Linked List Cycle
- 【LeetCode】Linked List Cycle
- Linked List Cycle - LeetCode
- 【LeetCode】Linked List Cycle
- yii2 rabc 的使用和配置
- AdaBoost--从原理到实现
- 内表数据写到服务器上
- java正则表达式
- Window 下安装consul
- 4.24 leetcode -24 linked-list-cycle
- 突然发现Bootstrsp的响应式工具挺好用的
- Qmake recursion
- CSS的预编译——less语言基本语法教程(入门)
- 控制反转(IoC)技术在C++中的使用
- Java 极光推送分组推送
- IAR Error[Pe020]: identifier 'FILE' is undefined 解决方法
- ubuntu14.04.1安装chrome 浏览器
- Windows Server2008 R2 设置允许多个人远程连接桌面