Home >Backend Development >Python Tutorial >How Do I Implement a Linked List in Python?
This article details Python's linked list implementation using Node and LinkedList classes. It covers insertion, deletion, and traversal, comparing linked lists to other data structures. The main focus is on linked lists' advantages in dynamic scen
Implementing a linked list in Python involves creating a Node
class to represent each element and a LinkedList
class to manage the list as a whole. Each Node
contains the data and a pointer to the next node in the sequence. The LinkedList
class typically includes methods for insertion, deletion, searching, and traversal.
Here's a basic implementation:
<code class="python">class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return current = self.head while current.next: current = current.next current.next = new_node def prepend(self, data): new_node = Node(data) new_node.next = self.head self.head = new_node def delete_node(self, key): current = self.head if current and current.data == key: self.head = current.next current = None return prev = None while current and current.data != key: prev = current current = current.next if current is None: return prev.next = current.next current = None def print_list(self): current = self.head while current: print(current.data, end=" -> ") current = current.next print("None") #Example Usage llist = LinkedList() llist.append(1) llist.append(2) llist.append(3) llist.prepend(0) llist.delete_node(2) llist.print_list() # Output: 0 -> 1 -> 3 -> None </code>
This example shows a singly linked list (each node points only to the next). Doubly linked lists (nodes point to both the next and previous nodes) are also possible, offering different performance characteristics for certain operations.
Advantages:
Disadvantages:
Compared to other data structures like arrays, Python lists (which are dynamic arrays), stacks, queues, and trees, linked lists excel when frequent insertions and deletions are needed at arbitrary positions. However, if random access is crucial, arrays or Python lists are much better choices.
Searching and deleting nodes in a linked list inherently involves traversal. Efficient searching typically means minimizing the number of nodes visited. For a singly linked list, searching is inherently linear, O(n) time complexity. Deleting a node requires finding the node to be deleted and then updating the pointers of its predecessor and successor.
The delete_node
method in the previous code example demonstrates a linear-time deletion. To improve efficiency for searching, you could consider using a self-balancing binary search tree or a hash table if you frequently need to search for specific nodes. However, these would require a significant restructuring of your data storage.
Linked lists find applications in scenarios where dynamic insertion and deletion are more important than random access:
In essence, whenever the cost of shifting elements in an array (due to frequent insertions/deletions) outweighs the cost of sequential access, a linked list is a strong contender.
The above is the detailed content of How Do I Implement a Linked List in Python?. For more information, please follow other related articles on the PHP Chinese website!