第八周作业
时间复杂度为 O(m+2n),空间复杂度 O(1)
代码片段
function findMergeNode(link1, link2)
result <- null
m <- link1.length
n <- link2.length
differ <- m > n ? m - n : m -n
[shortLink, longList] <- m > n ? [link1, lnk2] : [link2, link1]
while differ-- > 0 do
longLink <- longLink.next
end while
while longLink do
if longLink == shortLink then
result <- longList
else
longLink <- longLink.next
shortLink <- shortLink.next
end if
end while
end function
评论