|
| 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 | +# Template for the linked list |
| 9 | +class LinkedList: |
| 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 |
| 15 | + # of a linked list. |
| 16 | + def insert_node_at_head(self, node): |
| 17 | + if self.head: |
| 18 | + node.next = self.head |
| 19 | + self.head = node |
| 20 | + else: |
| 21 | + self.head = node |
| 22 | + |
| 23 | + # create_linked_list method will create the linked list using the |
| 24 | + # given integer array with the help of InsertAthead method. |
| 25 | + def create_linked_list(self, lst): |
| 26 | + for x in reversed(lst): |
| 27 | + new_node = LinkedListNode(x) |
| 28 | + self.insert_node_at_head(new_node) |
| 29 | + |
| 30 | + # returns the number of nodes in the linked list |
| 31 | + def get_length(self, head): |
| 32 | + temp = head |
| 33 | + length = 0 |
| 34 | + while(temp): |
| 35 | + length+=1 |
| 36 | + temp = temp.next |
| 37 | + return length |
| 38 | + |
| 39 | + # returns the node at the specified position(index) of the linked list |
| 40 | + def get_node(self, head, pos): |
| 41 | + if pos != -1: |
| 42 | + p = 0 |
| 43 | + ptr = head |
| 44 | + while p < pos: |
| 45 | + ptr = ptr.next |
| 46 | + p += 1 |
| 47 | + return ptr |
| 48 | + |
| 49 | + # __str__(self) method will display the elements of linked list. |
| 50 | + def __str__(self): |
| 51 | + result = "" |
| 52 | + temp = self.head |
| 53 | + while temp: |
| 54 | + result += str(temp.data) |
| 55 | + temp = temp.next |
| 56 | + if temp: |
| 57 | + result += ", " |
| 58 | + result += "" |
| 59 | + return result |
| 60 | + |
| 61 | + |
| 62 | +def print_list_with_forward_arrow(linked_list_node): |
| 63 | + temp = linked_list_node |
| 64 | + while temp: |
| 65 | + print(temp.data, end=" ") # print node value |
| 66 | + |
| 67 | + temp = temp.next |
| 68 | + if temp: |
| 69 | + print("→", end=" ") |
| 70 | + else: |
| 71 | + # if this is the last node, print null at the end |
| 72 | + print("→ null", end=" ") |
| 73 | + |
| 74 | +def print_list_with_forward_arrow_loop(linked_list_node): |
| 75 | + temp = linked_list_node |
| 76 | + while temp: |
| 77 | + print(temp.data, end=" ") # print node value |
| 78 | + |
| 79 | + temp = temp.next |
| 80 | + if temp: |
| 81 | + print("→", end=" ") |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +# DETECT CYCLE |
| 88 | +def detect_cycle(head): |
| 89 | + |
| 90 | + # Initialize slow and fast pointers at the head |
| 91 | + slow_pointer = head |
| 92 | + fast_pointer = head |
| 93 | + |
| 94 | + # Continue moving pointers as long as end of list not found |
| 95 | + while fast_pointer is not None and fast_pointer.next is not None: |
| 96 | + slow_pointer = slow_pointer.next |
| 97 | + fast_pointer = fast_pointer.next.next |
| 98 | + |
| 99 | + # If cycle is found, return True |
| 100 | + if slow_pointer == fast_pointer: |
| 101 | + return True |
| 102 | + |
| 103 | + # If end of list found return False |
| 104 | + return False |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +# Driver code |
| 111 | +def main(): |
| 112 | + |
| 113 | + input = ( |
| 114 | + [2, 4, 6, 8, 10, 12], |
| 115 | + [1, 3, 5, 7, 9, 11], |
| 116 | + [0, 1, 2, 3, 4, 6], |
| 117 | + [3, 4, 7, 9, 11, 17], |
| 118 | + [5, 1, 4, 9, 2, 3], |
| 119 | + ) |
| 120 | + pos = [0, -1, 1, -1, 2] |
| 121 | + j = 1 |
| 122 | + |
| 123 | + for i in range(len(input)): |
| 124 | + |
| 125 | + input_linked_list = LinkedList() |
| 126 | + input_linked_list.create_linked_list(input[i]) |
| 127 | + print(f"{j}.\tInput: ", sep="", end="") |
| 128 | + if pos[i] == -1: |
| 129 | + print_list_with_forward_arrow(input_linked_list.head) |
| 130 | + else: |
| 131 | + print_list_with_forward_arrow_loop(input_linked_list.head) |
| 132 | + print("\n\tpos:", pos[i]) |
| 133 | + if pos[i] != -1: |
| 134 | + length = input_linked_list.get_length(input_linked_list.head) |
| 135 | + last_node = input_linked_list.get_node(input_linked_list.head, length - 1) |
| 136 | + last_node.next = input_linked_list.get_node(input_linked_list.head, pos[i]) |
| 137 | + |
| 138 | + print(f"\n\tDetected cycle = {detect_cycle(input_linked_list.head)}") |
| 139 | + j += 1 |
| 140 | + print("-"*100, "\n") |
| 141 | + |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + main() |
| 145 | + |
0 commit comments