Day 3 - Linked ListsSingly Linked List

Basic Operations on a Singly Linked List

We can perform the following basic operations on a Singly Linked List:

  1. Define a Linked List
  2. Insert an Element at the beginning
  3. Insert an Element at the end
  4. Insert an Element after a given position
  5. Insert an Element before a given position
  6. Delete an Element at the beginning
  7. Delete an Element at the end
  8. Delete an Element after a given position
  9. Delete an Element before a given position
  10. Count the number of Elements/Nodes
  11. Traverse and Display the Elements/Nodes
  12. Reverse the Linked List

Complexity at a Glance

OperationTimeSpaceNotes
Access by index (traverse to position k)O(n)O(1)No random access — must follow pointers
Search (find by value)O(n)O(1)Must scan from HEAD in worst case
Insert at beginningO(1)O(1)Just update HEAD pointer
Insert at end (no tail ptr)O(n)O(1)Must traverse to find last node
Insert at end (with tail ptr)O(1)O(1)Directly update TAIL pointer
Insert at position kO(n)O(1)Traverse to k-1, then rewire pointers
Delete at beginningO(1)O(1)Move HEAD to HEAD→next
Delete at endO(n)O(1)Traverse to second-to-last node
Delete at position kO(n)O(1)Traverse to k-1, bypass node k
Count nodesO(n)O(1)Single pass from HEAD to NULL
ReverseO(n)O(1)Three-pointer iterative approach

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: EXIT

This 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?