
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add Element to First and Last Position of Linked List in Python
In Python, a linked list is a linear data structure that consists of a chain of nodes, where each node contains a value and a reference to the next node in the chain.
In this article, we will be discussing how to add an element to the first and last position of a linked list in Python.
Linked List in Python
A linked list is a referential data structure that holds a group of elements. it is similar in terms of an array but while the data is stored in contiguous memory location in an array, in linked list the data is not bound by that condition. That means the data, instead of being stored one after another, is stored in a random manner in memory.
This raises one question that is, how we can access the elements in a linked list? The answer is quite intuitive in linked list one element points to another till the end of the list.
The beginning and the end of the list are considered special positions. The beginning of the list is called its head and it points to the first element and the last element is special in the sense as it points to NULL.
Head -> data_1 -> data_2 -> ? -> data_n -> NULL
Now that we know how we can access the beginning and the end of the linked list, let us see how we can traverse elements and access the data in a linked list.
Traversing the linked list is quite simple, we just start from head and access the next node; we keep doing this process, until we find a node whose next node is NULL. As for accessing data in the nodes we use the arrow operator, "->".
Head->data
Now we have all the required understanding to start approaching the problem.
Adding elements at the beginning
To add the data at the beginning of the linked list, we must take into consideration the head of the linked list. Whenever we add a node at the beginning of the linked list, the linked list will be modified with the newly added node being the first node / head of the list.
Algorithm
Step 1 - Create the new node
Step 2 - Add the data in the newly created node
Step 3 - Update the link of the new node and make it point to current head node
Step 4 - Now make the head pointer equal to the newly created node
Note - The sequence of these steps matter the most, as if you make the newly created node the head node first, and then we will have no way to update the link of the new node which should ideally be pointing to the previous head node.
Example
class Node: def __init__(self, data): self.dataPart = data self.nextNode = None class LinkedList: def __init__(self): self.headNode = None def showList(self): n = self.headNode while n is not None: print(n.dataPart, end='-') n = n.nextNode print('') def addBeginList(self, data): tempNode = Node(data) tempNode.nextNode = self.headNode self.headNode = tempNode newLinkedList = LinkedList() print("Printing the list before adding element : ") newLinkedList.showList() newLinkedList.addBeginList(10) newLinkedList.addBeginList(25) print("Printing the elements after adding at the beginning of the list") newLinkedList.showList()
Output
Printing the list before adding any element : \ Printing the elements after adding at the beginning of the list 25-10-\
Adding elements at the end
Adding elements at the end, is logically different from adding at the beginning of the list. This time we need to access the last node of the list instead of the first node, i.e., head.
Now the problem is to check whether the list we are trying to add elements into is an empty list or does it already have some elements in it.
If the list is empty then the new node will be the first node for the list, and in the other case, it will be the last node. For that we need to check whether the head node is None or not. The list is treated empty of head is None, and not empty otherwise.
Algorithm
Step 1 - Create a new node.
Step 2 - Add the data in the data part of the node.
Step 3 - Make sure the next node of the newly created node points to None or Null pointer.
Step 4 - If the list is empty, make the newly created node as the head node.
Step 5 - Else traverse to the end of list, last node.
Step 6 - Set the next node of the last node to the newly created node.
Example
class Node: def __init__(self, data): self.dataPart = data self.nextNode = None class LinkedList: def __init__(self): self.headNode = None def showList(self): n = self.headNode while n is not None: print(n.dataPart, end='-') n = n.nextNode print("") def addEndList(self, data): tempNode = Node(data) if self.headNode is None: self.headNode = tempNode else: n = self.headNode while n.nextNode is not None: n = n.nextNode n.nextNode = tempNode newLinkedList = LinkedList() print("Printing the list before insertion : ") newLinkedList.showList() newLinkedList.addEndList(25) newLinkedList.addEndList(10) print("Printing the list after adding elements at the end of the list : ") newLinkedList.showList()
Output
Printing the list before insertion : \ Printing the list after adding elements at the end of the list : 25-10-\
Conclusion
In this article, we discussed how we can use python classes to implement a linked list, and how to add elements to the linked list. We focused on adding elements at the beginning as well as at the end of the list.