Skip to content

Readability improvements to singly_linked_list.py #1403

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

Merged
merged 1 commit into from
Oct 19, 2019
Merged
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
58 changes: 29 additions & 29 deletions data_structures/linked_list/singly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,56 @@ def __init__(self, data):

class Linked_List:
def __init__(self):
self.Head = None # Initialize Head to None
self.head = None # Initialize head to None

def insert_tail(self, data):
if self.Head is None:
if self.head is None:
self.insert_head(data) # If this is first node, call insert_head
else:
temp = self.Head
temp = self.head
while temp.next != None: # traverse to last node
temp = temp.next
temp.next = Node(data) # create node & link to tail

def insert_head(self, data):
newNod = Node(data) # create a new node
if self.Head != None:
newNod.next = self.Head # link newNode to head
self.Head = newNod # make NewNode as Head
if self.head != None:
newNod.next = self.head # link newNode to head
self.head = newNod # make NewNode as head

def printList(self): # print every node data
tamp = self.Head
while tamp is not None:
print(tamp.data)
tamp = tamp.next
temp = self.head
while temp is not None:
print(temp.data)
temp = temp.next

def delete_head(self): # delete from head
temp = self.Head
if self.Head != None:
self.Head = self.Head.next
temp = self.head
if self.head != None:
self.head = self.head.next
temp.next = None
return temp

def delete_tail(self): # delete from tail
tamp = self.Head
if self.Head != None:
if self.Head.next is None: # if Head is the only Node in the Linked List
self.Head = None
temp = self.head
if self.head != None:
if self.head.next is None: # if head is the only Node in the Linked List
self.head = None
else:
while tamp.next.next is not None: # find the 2nd last element
tamp = tamp.next
tamp.next, tamp = (
while temp.next.next is not None: # find the 2nd last element
temp = temp.next
temp.next, temp = (
None,
tamp.next,
) # (2nd last element).next = None and tamp = last element
return tamp
temp.next,
) # (2nd last element).next = None and temp = last element
return temp

def isEmpty(self):
return self.Head is None # Return if Head is none
return self.head is None # Return if head is none

def reverse(self):
prev = None
current = self.Head
current = self.head

while current:
# Store the current node's next node.
Expand All @@ -67,15 +67,15 @@ def reverse(self):
# Make the current node the next node (to progress iteration)
current = next_node
# Return prev in order to put the head at the end
self.Head = prev
self.head = prev


def main():
A = Linked_List()
print("Inserting 1st at Head")
print("Inserting 1st at head")
a1 = input()
A.insert_head(a1)
print("Inserting 2nd at Head")
print("Inserting 2nd at head")
a2 = input()
A.insert_head(a2)
print("\nPrint List : ")
Expand All @@ -88,7 +88,7 @@ def main():
A.insert_tail(a4)
print("\nPrint List : ")
A.printList()
print("\nDelete Head")
print("\nDelete head")
A.delete_head()
print("Delete Tail")
A.delete_tail()
Expand Down