Foundation·Concept·2 min read

Linked list in Python

Practice this question

Node structure

Python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

Problems to practice

  • Reverse Linked List
  • Middle of the Linked List
  • Remove Nth Node from End
  • Merge Two Sorted Lists
  • Detect Cycle
  • Intersection of Two Linked Lists

Example: reverse linked list

Python
def reverse_list(head):
    prev = None
    curr = head

    while curr:
        nxt = curr.next
        curr.next = prev
        prev = curr
        curr = nxt

    return prev

Adapted from 18_DSA_PYTHON.md · “4. Linked list in Python”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.