【LeetCode】Linked List Cycle

来源:互联网 发布:信捷plc 编程软件3.5 编辑:程序博客网 时间:2024/05/16 14:01
/** * 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 *one, *two;if (head == NULL || head->next == NULL)return false;one = head; two = head;while (two && two->next && two->next->next){one = one->next;two = two->next->next;if (one == two){return true; }}return false;    }};

0 0
原创粉丝点击