|
| 1 | +'''Question: |
| 2 | +Write code to remove duplicates from an unsorted linked list. FOLLOW UP |
| 3 | +How would you solve this problem if a temporary buffer is not allowed? |
| 4 | +
|
| 5 | +clarification: |
| 6 | +- singly linked list? yes |
| 7 | +- will there be circles? no |
| 8 | +- can I assume the linked list only contains integer data? yes |
| 9 | +- if thare are dups at position x and x+n, remove the one at x+n |
| 10 | +- the order should be reserved |
| 11 | +- what is a temporary buffer? it means even hash table cannot be used |
| 12 | +- return head |
| 13 | +''' |
| 14 | + |
| 15 | +import unittest |
| 16 | +from test_utils import UnitTestBase |
| 17 | +from data_structure import Node, LinkedList as LL |
| 18 | + |
| 19 | +# Solution: use hash table |
| 20 | +# Time: O(n) |
| 21 | +# Space: O(n) |
| 22 | +def remove_dups_1(ll): |
| 23 | + cur = ll.head |
| 24 | + if cur == None: |
| 25 | + return ll |
| 26 | + |
| 27 | + keys = set([cur.data]) |
| 28 | + while cur.next != None: |
| 29 | + key = cur.next.data |
| 30 | + if key not in keys: |
| 31 | + keys.add(key) |
| 32 | + cur = cur.next |
| 33 | + else: |
| 34 | + cur.next = cur.next.next |
| 35 | + |
| 36 | + return ll |
| 37 | + |
| 38 | +# Solution: no temporaty buffer |
| 39 | +# Time: O(n*n) |
| 40 | +# Space: O(1) |
| 41 | +def remove_dups_2(ll): |
| 42 | + anchor = ll.head |
| 43 | + while anchor != None: |
| 44 | + cur, key = anchor, anchor.data |
| 45 | + |
| 46 | + while cur.next != None: |
| 47 | + if cur.next.data != key: |
| 48 | + cur = cur.next |
| 49 | + else: |
| 50 | + cur.next = cur.next.next |
| 51 | + |
| 52 | + anchor = anchor.next |
| 53 | + |
| 54 | + return ll |
| 55 | + |
| 56 | +# Solution: no temporaty buffer. recursion |
| 57 | +# Time: O(n*n) |
| 58 | +# Space: O(1) - tail recursion |
| 59 | +def remove_dups_3(ll): |
| 60 | + remove_dups_3_helper(ll.head) |
| 61 | + return ll |
| 62 | + |
| 63 | +def remove_dups_3_helper(anchor): |
| 64 | + if anchor == None: |
| 65 | + return |
| 66 | + |
| 67 | + cur, key = anchor, anchor.data |
| 68 | + |
| 69 | + while cur.next != None: |
| 70 | + if cur.next.data != key: |
| 71 | + cur = cur.next |
| 72 | + else: |
| 73 | + cur.next = cur.next.next |
| 74 | + |
| 75 | + remove_dups_3_helper(anchor.next) |
| 76 | + |
| 77 | +class Test(UnitTestBase, unittest.TestCase): |
| 78 | + def data_provider(self): |
| 79 | + return [ |
| 80 | + (LL(), LL()), |
| 81 | + (LL([1]), LL([1])), |
| 82 | + (LL([1, 2, 3]), LL([1, 2, 3])), |
| 83 | + (LL([1, 1]), LL([1])), |
| 84 | + (LL([4, 8, 2, 4]), LL([4, 8, 2])), |
| 85 | + (LL([4, 2, 2, 4]), LL([4, 2])), |
| 86 | + (LL([1, 4, 6, 8, 6, 2, 1]), LL([1, 4, 6, 8, 2])), |
| 87 | + (LL([4, 2, 3, 4, 8, 6, 6, 2]), LL([4, 2, 3, 8, 6])), |
| 88 | + ] |
| 89 | + |
| 90 | + def func_provider(self): |
| 91 | + return [ |
| 92 | + remove_dups_1, |
| 93 | + remove_dups_2, |
| 94 | + remove_dups_3, |
| 95 | + ] |
| 96 | + |
| 97 | + def func_eval(self, func, args): |
| 98 | + return func(args) |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + unittest.main() |
0 commit comments