| 8 | # Returns true if first list is |
| 9 | # present in second list |
| 10 | def findList(first, second): |
| 11 | |
| 12 | # If both linked lists are empty/None, |
| 13 | # return True |
| 14 | if not first and not second: |
| 15 | return True |
| 16 | |
| 17 | # If ONLY one of them is empty, |
| 18 | # return False |
| 19 | if not first or not second: |
| 20 | return False |
| 21 | |
| 22 | ptr1 = first |
| 23 | ptr2 = second |
| 24 | |
| 25 | # Traverse the second LL by |
| 26 | # picking nodes one by one |
| 27 | while ptr2: |
| 28 | |
| 29 | # Initialize 'ptr2' with current |
| 30 | # node of 'second' |
| 31 | ptr2 = second |
| 32 | |
| 33 | # Start matching first LL |
| 34 | # with second LL |
| 35 | while ptr1: |
| 36 | |
| 37 | # If second LL become empty and |
| 38 | # first not, return False, |
| 39 | # since first LL has not been |
| 40 | # traversed completely |
| 41 | if not ptr2: |
| 42 | return False |
| 43 | |
| 44 | # If value of both nodes from both |
| 45 | # LLs are equal, increment pointers |
| 46 | # for both LLs so that next value |
| 47 | # can be matched |
| 48 | elif ptr1.value == ptr2.value: |
| 49 | ptr1 = ptr1.next |
| 50 | ptr2 = ptr2.next |
| 51 | |
| 52 | # If a single mismatch is found |
| 53 | # OR ptr1 is None/empty,break out |
| 54 | # of the while loop and do some checks |
| 55 | else: |
| 56 | break |
| 57 | |
| 58 | # check 1 : |
| 59 | # If 'ptr1' is None/empty,that means |
| 60 | # the 'first LL' has been completely |
| 61 | # traversed and matched so return True |
| 62 | if not ptr1: |
| 63 | return True |
| 64 | |
| 65 | # If check 1 fails, that means, some |
| 66 | # items for 'first' LL are still yet |
| 67 | # to be matched, so start again by |