Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Implementation of Singly Linked List in Python #100

Merged
merged 2 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions python/Linked List/README.md
Original file line number Diff line number Diff line change
@@ -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.
101 changes: 101 additions & 0 deletions python/Linked List/Singly_LinkedList.py
Original file line number Diff line number Diff line change
@@ -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