Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mo-Shakib/88d1ce5a8c9e1826ecdff9356f3f8f7d to your computer and use it in GitHub Desktop.
Save Mo-Shakib/88d1ce5a8c9e1826ecdff9356f3f8f7d to your computer and use it in GitHub Desktop.
Python - Insert a new node at the end of the Linked List
def insert_at_end(self, newElement):
#1 & 2 & 3. allocate node, assign data element
# assign null to the next of new node
newNode = Node(newElement)
#4. Check the Linked List is empty or not,
# if empty make the new node as head
if(self.head == None):
self.head = newNode
return
else:
#5. Else, traverse to the last node
temp = self.head
while(temp.next != None):
temp = temp.next
#6. Change the next of last node to new node
temp.next = newNode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment