Why Singly Linked Lists Matter
They test:- Understanding of pointers
- Recursion
- Iteration
- Edge case handling (null nodes, head/tail issues)
- Space and time complexity reasoning
Basic Operations on Singly Linked List
1. Insert at Head
Pseudocode:
function insertAtHead(head, value):
newNode = Node(value)
newNode.next = head
return newNode // new head
Python Snippet:
class Node:
def __init__(self, val):
self.val = val
self.next = None
def insertAtHead(head, value):
newNode = Node(value)
newNode.next = head
return newNode
2. Insert at Tail
Pseudocode:
function insertAtTail(head, value):
newNode = Node(value)
if head is null:
return newNode
temp = head
while temp.next != null:
temp = temp.next
temp.next = newNode
return head
Python Snippet:
def insertAtTail(head, value):
newNode = Node(value)
if not head:
return newNode
temp = head
while temp.next:
temp = temp.next
temp.next = newNode
return head
3. Delete from Head
Pseudocode:
function deleteFromHead(head):
if head is null:
return null
return head.next
Python Snippet:
def deleteFromHead(head):
if not head:
return None
return head.next
4. Delete from Tail
Pseudocode:
function deleteFromTail(head):
if head is null or head.next is null:
return null
temp = head
while temp.next.next != null:
temp = temp.next
temp.next = null
return head
Python Snippet:
def deleteFromTail(head):
if not head or not head.next:
return None
temp = head
while temp.next.next:
temp = temp.next
temp.next = None
return head
5. Search a Value
Pseudocode:
function search(head, target):
temp = head
while temp != null:
if temp.val == target:
return true
temp = temp.next
return false
Python Snippet:
def search(head, target):
temp = head
while temp:
if temp.val == target:
return True
temp = temp.next
return False
6. Reverse a Linked List
Pseudocode:
function reverseList(head):
prev = null
curr = head
while curr != null:
next = curr.next
curr.next = prev
prev = curr
curr = next
return prev // new head
Python Snippet:
def reverseList(head):
prev = None
curr = head
while curr:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
return prev
7. Length of Linked List
Pseudocode:
function getLength(head):
count = 0
temp = head
while temp != null:
count += 1
temp = temp.next
return count
Python Snippet:
def getLength(head):
count = 0
temp = head
while temp:
count += 1
temp = temp.next
return count
8. Print Linked List
Pseudocode:
function printList(head):
temp = head
while temp != null:
print temp.val
temp = temp.next
Python Snippet:
def printList(head):
temp = head
while temp:
print(temp.val, end=" -> ")
temp = temp.next
print("None")
Beginner-Level Singly Linked List Questions (FAANG-Friendly)