141. Linked List Cycle
来源:互联网 发布:中国技术壁垒知乎 编辑:程序博客网 时间:2024/04/30 11:31
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
快慢指针,但不额外占用空间的方法没找到。
/** * 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) { struct ListNode* p1 = head; struct ListNode* p2 = head; while (p1 && p1->next) { p1 = p1->next->next; p2 = p2->next; if (p1 == p2) return true; } return false; }};
0 0
- 141.Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141.Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- 141. Linked List Cycle
- Linux中vi工具的常用命令
- 2016facebook入门题c
- RTP-RTCP协议分析
- 教大家写几个可能用得上的css3简单动画
- Android中实现全屏、无标题栏的两种办法(另附Android系统自带样式的解释)(转)
- 141. Linked List Cycle
- WPF教程(八)WPF应用简介
- java语法糖 泛型与类型擦除
- hdu 1509 优先队列
- c++中vector的用法详解
- android学习笔记(八)SharedPreferences、editor .
- 振南的znFAT FAT32文件系统
- 排序算法 -- 堆排序
- 注解@Component,@Controller,@Service,@Repository