Skip to content

Commit 1e753da

Browse files
authored
Update nonCoprimes.vn.py
1 parent 0b6a3e5 commit 1e753da

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

leetcode/nonCoprimes/nonCoprimes.vn.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ def pgcd(a, b): return gcd(a, b)
3838
def ppcm(a, b): return a * b // pgcd(a, b)
3939

4040
now = res.head
41-
while now and now.next:
41+
while now != res.tail:
42+
# If we got to the tail, we're done
4243
nxt = now.next
4344
if pgcd(now.val, nxt.val) > 1:
4445
new = Node(ppcm(now.val, nxt.val))
45-
46+
# Update the links to replace old nodes by one
4647
if nxt != res.tail:
4748
nxt.next.prev = new
4849
new.next = nxt.next
@@ -58,4 +59,4 @@ def ppcm(a, b): return a * b // pgcd(a, b)
5859
else:
5960
now = now.next
6061

61-
return list(res)
62+
return list(res)

0 commit comments

Comments
 (0)