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 | 432 Views, 23641 Search Bots | 120 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 432 Views, 23641 Search Bots | 120 Words
| 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.
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 432 Views, 23641 Search Bots | 120 Words 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
Related Articles
- Algorithm Interview: Determine If Linked List is Palindrome
- Daily Interview Problem: Tree Serialization
- Daily Interview Question: Find Cycles in a Graph
- Two Tricks of Delphi
- Daily Interview Problem: Running Median
- Daily Interview Question: Edit Distance
- Daily Interview Problem: Merge K Sorted Linked Lists
- Palindrome Integers
- Algorithm Interview: Convert Roman Numerals to Decimal
- Non-decreasing Array with Single Modification
©2006~2024 SteakOverCooked - 0.01032 Seconds(s) - 5280.706 KB/s - 12 Online Memory: 517.3 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !