Linked List Cycle

来源:互联网 发布:西门子编程电缆价格 编辑:程序博客网 时间:2024/04/30 08:59

Linked List Cycle


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

Follow up:
Can you solve it without using extra space?

参考代码:http://leetcode.com/2010/09/detecting-loop-in-singly-linked-list.html 


bool hasCycle(ListNode *head) {//经典解法        ListNode* slow =head,*fast =head;        while(fast&&fast->next)        {            slow =slow->next;            fast =fast->next->next;            if(slow ==fast)                return true;        }        return false;    }


0 0
原创粉丝点击