|
| 1 | +class LinkedListNode: |
| 2 | + # __init__ will be used to make a LinkedListNode type object |
| 3 | + def __init__(self, data, next=None): |
| 4 | + self.data = data |
| 5 | + self.next = next |
| 6 | + |
| 7 | + |
| 8 | +class LinkedList: |
| 9 | + |
| 10 | + # __init__ will be used to make a LinkedList type object |
| 11 | + def __init__(self): |
| 12 | + self.head = None |
| 13 | + |
| 14 | + # insert_node_at_head method will insert a LinkedListNode at head of a linked list |
| 15 | + def insert_node_at_head(self, node): |
| 16 | + if self.head: |
| 17 | + node.next = self.head |
| 18 | + self.head = node |
| 19 | + else: |
| 20 | + self.head = node |
| 21 | + |
| 22 | + # create_linked_list method will create the linked list using the given |
| 23 | + # integer array with the help of InsertAthead method |
| 24 | + def create_linked_list(self, lst): |
| 25 | + for x in reversed(lst): |
| 26 | + new_node = LinkedListNode(x) |
| 27 | + self.insert_node_at_head(new_node) |
| 28 | + |
| 29 | + |
| 30 | +def print_list_with_forward_arrow(linked_list_node): |
| 31 | + temp = linked_list_node |
| 32 | + while temp: |
| 33 | + print(temp.data, end=" ") # print node value |
| 34 | + |
| 35 | + temp = temp.next |
| 36 | + if temp: |
| 37 | + print("→", end=" ") |
| 38 | + else: |
| 39 | + # if this is the last node, print null at the end |
| 40 | + print("→ null", end=" ") |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +############################################################ |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +def remove_duplicates(head): |
| 49 | + |
| 50 | + # Initialize pointer to traverse through LinkedList |
| 51 | + current = head |
| 52 | + |
| 53 | + while current and current.next: |
| 54 | + |
| 55 | + # Check if duplicate detected |
| 56 | + if current.data == current.next.data: |
| 57 | + current.next = current.next.next |
| 58 | + |
| 59 | + # No duplicate |
| 60 | + else: |
| 61 | + current = current.next |
| 62 | + |
| 63 | + # Return the head of the modified linked list (with duplicates removed) |
| 64 | + return head |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +# Time Complexity = O(n) |
| 69 | +# Space Complexity = O(1) |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +############################################################ |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +# Driver code |
| 78 | +def main(): |
| 79 | + input_list = [ |
| 80 | + [1, 2, 2, 3, 3, 3], |
| 81 | + [-21, -21, -21, -21, -21, -21, -21], |
| 82 | + [3, 7, 9], |
| 83 | + [-100, -100, -100, -10, -10, 0, 10, 10, 100, 100, 100], |
| 84 | + [-77, -77, -7, -7, -7, -7, 7, 7, 7, 7, 77, 77, 77, 77] |
| 85 | + ] |
| 86 | + |
| 87 | + for i in range(len(input_list)): |
| 88 | + |
| 89 | + input_linked_list = LinkedList() |
| 90 | + input_linked_list.create_linked_list(input_list[i]) |
| 91 | + |
| 92 | + print(i + 1, ".\tInput: ", end="", sep="") |
| 93 | + print_list_with_forward_arrow(input_linked_list.head) |
| 94 | + |
| 95 | + print("\n\n\tOutput: ", end="", sep="") |
| 96 | + print_list_with_forward_arrow(remove_duplicates(input_linked_list.head)) |
| 97 | + |
| 98 | + print("\n", "-"*100, sep="") |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + main() |
| 102 | + |
0 commit comments