Basic Operations on a Singly Linked List
We can perform the following basic operations on a Singly Linked List:
- Define a Linked List
- Insert an Element at the beginning
- Insert an Element at the end
- Insert an Element after a given position
- Insert an Element before a given position
- Delete an Element at the beginning
- Delete an Element at the end
- Delete an Element after a given position
- Delete an Element before a given position
- Count the number of Elements/Nodes
- Traverse and Display the Elements/Nodes
- Reverse the Linked List
Complexity at a Glance
The key insight: insertion and deletion themselves are O(1) once you have a pointer to the right position. The O(n) cost comes from finding that position, not from the rewiring of pointers.
Define a Linked List
Insert an Element at the beginning
- ALGORITHM
Step 1: if AVAIL == NULL
Write Overflow
Go to step 7
[END OF if]
Step 2: SET newNode = AVAIL
Step 3: SET AVAIL = AVAIL -> next
Step 4: SET newNode -> data = val
Step 5: SET newNode -> next = start
Step 6: SET start = newNode
Step 7: EXIT- CODE
Insert an Element at the End
- ALGORITHM
Step 1: if AVAIL = NULL
Write Overflow
Go to step 10
[END OF if]
Step 2: SET newNode = AVAIL
Step 3: SET AVAIL = AVAIL->next
Step 4: SET newNode->data = val
Step 5: SET newNode->next = NULL
Step 6: SET last = start
Step 7: Repeat Step 8 while last->next != NULL
Step 8: SET last = last->next
[END OF LOOP]
Step 9: SET last->next = newNode
Step 10: EXIT- CODE
Insert an Element after a given position/element
- ALGORITHM
Step 1: if AVAIL = NULL
Write Overflow
Go to step 12
[END OF if]
Step 2: SET newNode = AVAIL
Step 3: SET AVAIL = AVAIL->next
Step 4: SET newNode->data = val
Step 5: SET ptr = start
Step 6: SET prePtr = ptr
Step 7: Repeat Steps 8 and 9 while prePtr->data != pos
Step 8: SET prePtr = ptr
Step 9: SET ptr = ptr->next
[END OF LOOP]
Step 10: SET prePtr->next = newNode
Step 11: SET newNode->next = ptr
Step 12: EXIT- CODE
Insert an Element before a given position/element
- ALGORITHM
Step 1: if AVAIL = NULL
Write Overflow
Go to step 12
[END OF if]
Step 2: SET newNode = AVAIL
Step 3: SET AVAIL = AVAIL->next
Step 4: SET newNode->data = val
Step 5: SET ptr = start
Step 6: SET prePtr = NULL
Step 7: Repeat Steps 8 and 9 while ptr->data != pos
Step 8: SET prePtr = ptr
Step 9: SET ptr = ptr->next
[END OF LOOP]
Step 10: SET prePtr->next = newNode
Step 11: SET newNode->next = ptr
Step 12: EXIT- CODE
Delete an Element at the Beginning
- ALGORITHM
Step 1: if start = NULL
Write Underflow
Go to step 5
[END OF if]
Step 2: SET ptr = start
Step 3: SET start = start->next
Step 4: free ptr
Step 5: EXIT- CODE
Delete an Element at the End
- ALGORITHM
Step 1: if start = NULL
Write Underflow
Go to step 8
[END OF if]
Step 2: SET ptr = start
Step 3: Repeat steps 4 to 5 while ptr->next != NULL
Step 4: SET prePtr = ptr
Step 5: SET ptr = ptr->next
[END OF LOOP]
Step 6: SET prePtr->next = NULL
Step 7: free ptr
Step 8: EXIT- CODE
Delete an Element after a given position
- ALGORITHM
Step 1: if start = NULL
Write Underflow
Go to step 10
[END OF if]
Step 2: SET ptr = start
Step 3: SET prePtr = ptr
Step 4: Repeat steps 5 to 6 while prePtr->data != val
Step 5: SET prePtr = ptr
Step 6: SET ptr = ptr->next
[END OF LOOP]
Step 7: SET temp = ptr
Step 8: SET prePtr->next = ptr->next
Step 9: free temp
Step 10: EXIT- CODE
Delete an Element before a given position
- ALGORITHM
Step 1: if start = NULL or start->next = NULL
Write Underflow (list too short)
Go to step 12
[END OF if]
Step 2: if start->next->data = pos
Write "No element before pos"
Go to step 12
[END OF if]
Step 3: SET ptr = start->next
Step 4: SET prePtr = start
Step 5: SET prePre = NULL
Step 6: Repeat Steps 7 to 9 while ptr->data != pos
Step 7: SET prePre = prePtr
Step 8: SET prePtr = ptr
Step 9: SET ptr = ptr->next
[END OF LOOP]
Step 10: SET prePre->next = ptr (bypass prePtr)
Step 11: free prePtr
Step 12: EXIT- CODE
Count the number of Elements/Nodes
- ALGORITHM
Step 1: [Initialize] SET ptr = start, count = 0
Step 2: Repeat Steps 3 to 4 while ptr != NULL
Step 3: Apply process to ptr->data
Step 4: SET ptr = ptr->next
SET count = count + 1
[END OF LOOP]
Step 5: EXIT- CODE
Traverse and Display the Elements/Nodes
- ALGORITHM
Step 1: [Initialize] SET ptr = start
Step 2: Repeat Steps 3 to 4 while ptr != NULL
Step 3: PRINT ptr->data
Step 4: SET ptr = ptr->next
[END OF LOOP]
Step 5: EXIT- CODE
Reverse a Linked List
- ALGORITHM
Step 1: SET current = start
Step 2: SET prev = NULL
Step 3: SET nextPtr = NULL
Step 4: Repeat steps 5 to 7 while current is not NULL
Step 5: SET nextPtr = current->next
Step 6: SET current->next = prev
Step 7: SET prev = current
SET current = nextPtr
[END OF LOOP]
Step 8: SET start = prev
Step 9: EXITThis is called the three-pointer technique. At each step: save next,
flip the arrow backward (current→prev), then advance both pointers. After
the loop, prev points to the new head.
- CODE
Quick Check
What is the time complexity of inserting a node at the BEGINNING of a singly linked list?
You need to delete the last node of a singly linked list with n nodes and NO tail pointer. What is the time complexity?
What is the THREE-POINTER technique used for in reversing a linked list?