Singly Linked List Insertion at End
The steps to insert a node at the end of the list are as follows:
- Create new node
- Set the new node's link to be Null
- Traverse the list, checking to see if the node's next link is Null.
- Change the last nodes link from Null to point to the new node.
Insertion at the end of a Singly Linked List is O(n) as the entire list had to be traversed.
Example:
Insert ruler at end of list
- Create node ruler
- Set ruler.next = null
- Traverse the List
- Is Head.link = Null? -No
- Is paper.link = Null? - No
- Is book.link = Null? - No
- Is pen.link = Null? - Yes
- Set pen.link = ruler
A new node, Ruler was added to the end of the list, and this is the new layout for the Singly Linked List.