leetcode 141. Linked List Cycle

xiaoxiao2021-02-28  21

原题:

Given a linked list, determine if it has a cycle in it.

代码如下;

/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ bool hasCycle(struct ListNode *head) { if(head==NULL) return false; struct ListNode* p1; struct ListNode* p2; p1=head; p2=head; while(p2->next&&p2->next->next) { p1=p1->next; p2=p2->next->next; if(p1==p2) return true; } return false; } 写两个差异指针就好咯。

转载请注明原文地址: https://www.6miu.com/read-1000244.html

最新回复(0)