Created
July 17, 2021 22:22
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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