diff --git a/python/Linked List/README.md b/python/Linked List/README.md new file mode 100644 index 000000000..ec6e9788f --- /dev/null +++ b/python/Linked List/README.md @@ -0,0 +1,17 @@ +# Linked List +A linked list is a sequence of data elements, which are connected together via links. Each data element contains a connection to another data element in form of a pointer. Python does not have linked lists in its standard library. We implement the concept of linked lists using the concept of nodes as discussed. +There are three common types of Linked List. + +# Singly Linked List :- +It is the most common. Each node has data and a pointer to the next node. + +# Doubly Linked List :- +We add a pointer to the previous node in a doubly-linked list. Thus, we can go in either direction: forward or backward. + +# Circular Linked List :- +A circular linked list is a variation of a linked list in which the last element is linked to the first element. This forms a circular loop. + +A circular linked list can be either singly linked or doubly linked. + +for singly linked list, next pointer of last item points to the first item +In the doubly linked list, prev pointer of the first item points to the last item as well. diff --git a/python/Linked List/Singly_LinkedList.py b/python/Linked List/Singly_LinkedList.py new file mode 100644 index 000000000..574ae0eda --- /dev/null +++ b/python/Linked List/Singly_LinkedList.py @@ -0,0 +1,101 @@ +class Node: + def __init__(self, item): + self.item = item + self.next = None + +class LinkedList: + def __init__(self): + self.head = None + + def to_linkedlist(self, input_list): + #Function to create a linked list from a input list. + self.tail = None + + for item in input_list: + if self.head is None: + self.head = Node(item) + self.tail = self.head + + else: + self.tail.next = Node(item) + self.tail = self.tail.next + + return self.head + + def append(self, item): + # function to add item to the end of linkedlist. + + if self.head is None: + self.head = Node(item) # if linkedlist is empty + return + + # Now , moving to the end of linkedlist to add item if linkedlist is not empty. + node = self.head + while node.next: + node = node.next + + node.next = Node(item) + return + + def prepend(self, item): + # function to add item at the front/beginning of the linkedlist. + if self.head is None: + self.head = Node(item) #when linkedlist is empty. + return + + new_node = Node(item) + new_node.next = self.head + self.head = new_node # changing head of linkedlist to new node when linkedlist is not empty. + + def pop(self): + # function returns the first node's item and remove it from the linkedlist. + if self.head is None: + return None + + node = self.head + self.head = self.head.next + + return node.item + + def remove(self, item): + # delete the first coming node with the desired item . + if self.head is None: + return None #when linkedlist is empty. + + if self.head.item == item: + self.head = self.head.next + return + + node = self.head + while node.next: + if node.next.item == item: + node.next = node.next.next + return + node = node.next + + print('Value not found') + + def size(self): + #function returns the size of the linkedlist. + size = 0 # initialising size with value 0. + node = self.head + while node: + size += 1 + node = node.next #Iterating through linkedlist. + + return size + + + + def to_list(self): + # Function to create a simple list from Linkedlist. + output_list = [] + + node = self.head + + while node: + output_list.append(node.item) + node = node.next # loop terminates at last item of linkedlist. + + return output_list +