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?

 

Subscribe to see which companies asked this question

/**
 * 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;
      p=head;
     // head->next=head;
 
       while(p!= NULL)
      {
        //(p->val)++;
        if(p->val==55)
        return true;
        p->val=55;
        p=p->next;
      }
      return false;
    }
};

人生有些关口非狠狠的斗一下不可,不能为了混口饭吃而自甘蹉跎。
原文地址:https://www.cnblogs.com/djiankuo/p/5008706.html