From 29cbbeaf1118f90a7838b85070a76cc65670fcbe Mon Sep 17 00:00:00 2001 From: temitayo Date: Tue, 23 Mar 2021 08:20:13 -0700 Subject: [PATCH] Add singly linked lists for python (#116) * fixed incorrect links * added singly list Data structure * removed semi-column --- linked-lists/Python/singly.py | 58 +++++++++++++++++++++++++++++++++++ linked-lists/README.md | 9 ++++-- 2 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 linked-lists/Python/singly.py diff --git a/linked-lists/Python/singly.py b/linked-lists/Python/singly.py new file mode 100644 index 00000000..de5037c1 --- /dev/null +++ b/linked-lists/Python/singly.py @@ -0,0 +1,58 @@ +# A simple Python program to create a singly linked list + +# Node class +class Node: + + # Function to initialise the node object + def __init__(self, data): + self.data = data # Assign data + self.next = None # Initialize next as null + + +# Linked List class contains a Node object +class LinkedList: + + # Function to initialize head + def __init__(self): + self.head = None + + + # Function to insert a new node at the beginning + def insertAtHead(self, new_data): + + # 1 & 2: Allocate the Node & + # Put in the data + new_node = Node(new_data) + + # 3. Make next of new Node as head + new_node.next = self.head + + # 4. Move the head to point to new Node + self.head = new_node + def removeAtHead(self): + temp = self.head + + # If head node itself holds the key to be deleted + if (temp is not None): + self.head = temp.next + temp = None + return + else: + return('underflow') + def printList(self): + temp = self.head + while(temp): + print (temp.data) + temp = temp.next + + + +# Code execution starts here +if __name__=='__main__': + l=LinkedList() + l.insertAtHead(1) + l.insertAtHead('xyz') + l.insertAtHead(1.1) + l.removeAtHead() + l.printList() + diff --git a/linked-lists/README.md b/linked-lists/README.md index 29a5cbef..1a774658 100644 --- a/linked-lists/README.md +++ b/linked-lists/README.md @@ -11,8 +11,11 @@ ### Java -1. [Singly Linked List](java/singly.cpp) +1. [Singly Linked List](java/singly.java) -### Java +### JavaScript -1. [Singly Linked List](js/singly.cpp) +1. [Singly Linked List](js/singly.js) + +### Python +1. [Singly Linked List](Python/singly.py)