FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ctci/python/Chapter 2/Question2_6.py at master · codevicky/ctci · GitHub
codevicky
/
ctci
Public
forked from
careercup/ctci
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
ctci
/
python
/
Chapter 2
/
Question2_6.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
46 lines (34 loc) · 1.09 KB
Breadcrumbs
ctci
/
python
/
Chapter 2
/
Question2_6.py
Copy path
File metadata and controls
46 lines (34 loc) · 1.09 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
35
36
37
38
39
40
41
42
43
44
45
46
from
classes
.
LinkedList
import
*
def
findBeginning
(
linkedlist
):
slow
=
linkedlist
.
head
fast
=
linkedlist
.
head
# Find meetng point
while
(
fast
!=
None
)
and
(
fast
.
next
!=
None
):
slow
=
slow
.
next
fast
=
fast
.
next
.
next
if
fast
==
slow
:
break
# Check whether it is a circular linked list
if
fast
==
None
or
fast
.
next
==
None
:
return
None
# Move one runner to head. Making them move at same pace, they will meet at the beginning of the loop
fast
=
linkedlist
.
head
while
fast
!=
slow
:
slow
=
slow
.
next
fast
=
fast
.
next
return
fast
# -----------------test------------------
nodes_number
=
100
nodes_in_loop
=
20
L
=
LinkedList
()
current
=
L
.
head
store
=
[]
# store nodes to help creating loop
# Create a linked list
for
i
in
range
(
nodes_number
):
L
.
addNode
(
i
)
current
=
L
.
head
if
i
==
0
else
current
.
next
store
.
append
(
current
)
# Creat loop
current
.
next
=
None
if
nodes_in_loop
<=
0
else
store
[
nodes_number
-
nodes_in_loop
]
beginning
=
findBeginning
(
L
)
print
beginning
# 80
Back
|
FazBrowse Home
|
New Git URL