To the Top
File:  root - text - article - 2020 - 02 - detect-linked-list-cycle.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 200 Views, 19774 Search Bots | 120 Words

Subscribe to Feed Burner | Browse | Archive
Hi, here's your problem today. This problem was recently asked by Uber:

Given a linked list, determine if the linked list has a cycle in it. For notation purposes, we use an integer pos which represents the zero-indexed position where the tail connects to.

Example:
Input: head = [4,3,2,1,0], pos = 1.
Output: true
The example indicates a list where the tail connects to the second node.


class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None

class Solution(object):
def hasCycle(self, head):
# Fill this in.

testHead = ListNode(4)
node1 = ListNode(3)
testHead.next = node1
node2 = ListNode(2)
node1.next = node2
node3 = ListNode(1)
node2.next = node3
testTail = ListNode(0)
node3.next = testTail
testTail.next = node1

print(Solution().hasCycle(testHead))
# True
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 200 Views, 19774 Search Bots | 120 Words Subscribe to Feed Burner

Related Articles

  1. Find the non-duplicate number
  2. Daily Interview Problem: Get all Values at a Certain Height in a Binary Tree
  3. [Daily Problem] Longest Palindromic Substring
  4. Daily Interview Problem: Given two arrays, write a function to compute their intersection.
  5. Daily Interview Problem: Tree Serialization
  6. Daily Interview Problem: Maximum Profit From Stocks
  7. Daily Interview Problem: Merge List Of Number Into Ranges
  8. Daily Interview Problem: First Missing Positive Integer
  9. Absolute Path
  10. Algorithm Interview: Subarray With Target Sum

Comments (0)

Your Email (Domain Part Not Exposed):

Your Comments:

Privately By Mail Colors More Smileys S x y @

Verification (Click Image 2 Refresh):

    Be the first one to comment this page !


Page Edited: October 30 2020 14:21:09 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />