0% found this document useful (0 votes)
2 views2 pages

Python LinkedLists Guide

A Linked List is a linear data structure consisting of nodes that contain data and a reference to the next node, differing from arrays in memory storage. The document outlines the node structure, how to create and traverse a linked list, and describes different types such as singly, doubly, and circular linked lists. It also highlights the advantages of dynamic sizing and efficient insertions/deletions, along with disadvantages like lack of random access and additional memory usage for pointers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Python LinkedLists Guide

A Linked List is a linear data structure consisting of nodes that contain data and a reference to the next node, differing from arrays in memory storage. The document outlines the node structure, how to create and traverse a linked list, and describes different types such as singly, doubly, and circular linked lists. It also highlights the advantages of dynamic sizing and efficient insertions/deletions, along with disadvantages like lack of random access and additional memory usage for pointers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Linked Lists in Python

A Linked List is a linear data structure where elements (nodes) are connected using pointers.
Each node contains data and a reference (link) to the next node in the sequence.
Unlike arrays, linked lists do not store elements in contiguous memory locations.

1. Node Structure
A node typically contains two parts:
- Data: The actual value.
- Next: Reference to the next node.
Example:
class Node:
def __init__(self, data):
self.data = data
self.next = None

2. Creating a Linked List


Example:
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
last = self.head
while last.next:
last = last.next
last.next = new_node

3. Traversing a Linked List


Example:
def display(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")

4. Types of Linked Lists


- Singly Linked List: Nodes have a link to the next node only.
- Doubly Linked List: Nodes have links to both the next and previous nodes.
- Circular Linked List: Last node points back to the first node.

5. Advantages & Disadvantages


Advantages:
- Dynamic size
- Efficient insertions/deletions
Disadvantages:
- No random access
- Extra memory for storing pointers

You might also like