-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinked List.py
More file actions
34 lines (29 loc) · 1.06 KB
/
Linked List.py
File metadata and controls
34 lines (29 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class _Node:
"""Lightweight, nonpublic class for storing a singly linked node."""
__slots__ = '_element','_next' # streamline memory usage
def __init__(self,element,next):
self._element = element
self._next = next
def reverse(head): # iterative
current,prev = head,None
while current:
next = current._next
current._next = prev
prev = current
current = next
head = prev
def reverse(head,p): # recursion
if p._next == None:
head = p
return
reverse(head,p._next)
q = p._next
q._next = p
p._next = None
def detectLoop(head): # Floyd's cycle-finding algorithm
slow,fast = head,head
while(slow and fast and fast._next)
slow,fast = slow._next,fast._next._next
if slow == fast:
return True
return False