首页 > 其他 > 详细

142. Linked List Cycle II

时间:2018-10-06 14:19:00      阅读:145      评论:0      收藏:0      [点我收藏+]

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Note: Do not modify the linked list.

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

# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        while head:
            if head.val == ‘ ‘:
                return head
            head.val = ‘ ‘
            head = head.next
        return None

我还是修改了链表..

142. Linked List Cycle II

原文:https://www.cnblogs.com/bernieloveslife/p/9745054.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!