Inconsistent C vs python result

  • 1

    I tried the following solution in C, got a runtime error:

    int hasCycle(struct ListNode *head) {
      if (!head || !(head->next))	{
        return 0;
      if (head->next == head)	{
        return 1;
      }else {
        head->next = head->next->next;
      return hasCycle(head->next);

    Tried the same solution in python, was accepted:

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    # = None
    class Solution(object):
        def hasCycle(self, head):
            :type head: ListNode
            :rtype: bool
            if not head or not
                return False
                return True
            return self.hasCycle(

    If modifying the list is not allowed, then the python solution should fail. If it is allowed, then the C solution should be Accepted.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.