Leetcode 141. Linked List Cycle (Easy) (cpp)
2016-07-12 12:47
483 查看
Leetcode 141. Linked List Cycle (Easy) (cpp)
Tag: Linked List, Two Pointers
Difficulty: Easy
Tag: Linked List, Two Pointers
Difficulty: Easy
/* 141. Linked List Cycle (Easy) 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) { ListNode *p = head; ListNode *q = head; while (p != NULL && q != NULL && p -> next != NULL) { p = p -> next -> next; q = q -> next; if (p == q) { return true; } } return false; } };
相关文章推荐
- C++打包DLL在C#中调用
- Leetcode 21. Merge Two Sorted Lists (Easy) (cpp)
- Leetcode 290. Word Pattern (Easy) (cpp)
- Leetcode 299. Bulls and Cows (Easy) (cpp)
- C++中的引用
- 【BZOJ1977】【BJOI2011】严格次小生成树
- Cpp环境【BZOJ1626】【Usaco2007 Dec】【Vijos 1693】 修建道路
- Leetcode 242. Valid Anagram (Easy) (cpp)
- Leetcode 350. Intersection of Two Arrays II (Easy) (cpp)
- Leetcode 349. Intersection of Two Arrays (Easy) (cpp)
- Leetcode 36. Valid Sudoku (Easy) (cpp)
- Leetcode 204. Count Primes (Easy) (cpp)
- Leetcode 66. Plus One (Easy) (cpp)
- Leetcode 121. Best Time to Buy and Sell Stock (Easy) (cpp)
- Leetcode 169. Majority Element (Easy) (cpp)
- C++中的auto_ptr智能指针的作用及使用方法详解
- Leetcode 283. Move Zeroes (Easy) (cpp)
- Leetcode 81. Search in Rotated Sorted Array II (Medium) (cpp)
- Leetcode 219. Contains Duplicate II (Easy) (cpp)
- Leetcode 217. Contains Duplicate (Easy) (cpp)