We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defCompareLists(headA,headB):whileTrue:# both nodes are Noneif(notheadA)and(notheadB):return1# only one node is None or node data differsif(bool(headA)!=bool(headB))or(headA.data!=headB.data):return0headA=headA.nextheadB=headB.next
First check if both nodes are None, which implies that all previous nodes (if any) are identical. If that's not the case, check if (only) one list is at it's tail or if the data of the current nodes differs. If neither is true, we can move both current nodes forward and repeat the loop.
Compare two linked lists
You are viewing a single comment's thread. Return to all comments →
I like to solve it in python too:
First check if both nodes are None, which implies that all previous nodes (if any) are identical. If that's not the case, check if (only) one list is at it's tail or if the data of the current nodes differs. If neither is true, we can move both current nodes forward and repeat the loop.