Singly Linked List Insertion in Middle



The steps to insert a node in the middle of the list are as follows:
  1. Create new node
  2. Traverse the list, checking to find the node to insert the new node after.
  3. Set the new node's link to be a copy of the current node's link
  4. Change the current nodes link to point to the new node.
Insertion in the middle of a Singly Linked List is O(n) as the entire list may have to be traversed.

Example:
Insert ruler after book
  1. Create node ruler
  2. Traverse the List
    1. Is Head = book? -No
    2. Is paper = book? - No
    3. Is book = book? - Yes
  3. Set ruler.link = pen
  4. Set book.link = ruler
A new node, Ruler was added to the middle of the list after the node book, and this is the new layout for the Singly Linked List.