141. Linked List Cycle(Linked List-Easy)
来源:互联网 发布:淘宝店铺访客特点 编辑:程序博客网 时间:2024/06/06 04:02
Description:Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
C代码:
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */bool hasCycle(struct ListNode *head) { struct ListNode *fast = (struct ListNode *)malloc(sizeof(struct ListNode)); struct ListNode *slow = (struct ListNode *)malloc(sizeof(struct ListNode)); fast = head; slow = head; while(fast != NULL && fast->next != NULL){ slow = slow->next; fast = fast->next->next; if(fast == slow){ return true; } } return false;}
C++代码::
/** * 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 *fast = head; ListNode *slow = head; while(fast != NULL && fast->next != NULL){ slow = slow->next; fast = fast->next->next; if(fast == slow){ return true; } } return false; }};
0 0
- 141. Linked List Cycle(Linked List-Easy)
- 141. Linked List Cycle (Easy)
- 141. Linked List Cycle [easy] (Python)
- Leetcode 141. Linked List Cycle (Easy) (cpp)
- 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
- 九度OJ-1080:进制转换
- void fun
- 平衡二叉树的四种调整方式和注意事项
- 15骑士周游问题(马踏棋盘问题)
- 利用C语言求一个数的所有质因数
- 141. Linked List Cycle(Linked List-Easy)
- 图(邻接矩阵存储)的广度优先遍历算法
- Linux---systemctl命令
- SQL面试题练习-实现pivot行列转换
- hdoj1092
- 【转载】SQL on Hadoop中用到的主要技术——MPP vs Runtime Framework
- 使用Keytool为JDK添加证书
- Mybatis学习笔记
- Mybatis学习笔记