Insertion Sort List
To solve this coding challenge, we will utilize the insertion sort algorithm to sort a singly linked list. The insertion sort algorithm is particularly effective for small or partially sorted lists, as it iteratively moves through the list, extracting elements one at a time and inserting them into their correct position in a growing sorted portion of the list.
# Explanation
Insertion sort works by dividing the list into a sorted and an unsorted region. Initially, the sorted region contains only the first element of the list. The algorithm then takes elements from the unsorted region one at a time and inserts them into their correct position within the sorted region. Hereβs a high-level approach to solving this problem:- Edge Case : If the list is empty or contains only one element, it is already sorted.
- Use a Dummy Node : Introduce a dummy node at the beginning to simplify edge case handling, particularly for elements that need to be inserted at the head.
- Iterate Through the List : Use pointers to traverse the list and perform the insertion sort.
- Find the Correct Position : For each node, find the appropriate position in the sorted region and insert it there.
- Update Pointers : Adjust the pointers to maintain the sorted region and continue the process until all elements are sorted.
- Initialization : Start with a dummy node to handle edge cases.
-
Traversal
: Traverse the list with a pointer (
current
- Condition Check : If the current node's value is less than or equal to the next node's value, move to the next node.
-
Node Repositioning
: If not, find the correct position in the sorted part using another pointer (
prev
-
Re-Insertion
: Remove the node from its current location and insert it after the
prev
- Repeat : Continue this process until the list is fully sorted.
Step-by-Step Explanation
Detailed Steps in Pseudocode
Function insertionSortList(head):
# Edge case: If head is null or only one element, return head
If head == null OR head.next == null:
Return head
# Create a dummy node to ease insertion at the head
dummy = new ListNode(0)
dummy.next = head
# Current node starts from the head
current = head
# Traverse the list with the current pointer
While current != null AND current.next != null:
# If the current value is less or equal to the next value, move to the next node
If current.val <= current.next.val:
current = current.next
# Otherwise, we need to insert current.next into the sorted part
Else:
# Remove the node to be repositioned
reposition_node = current.next
current.next = reposition_node.next
# Find the correct position in the sorted part
prev = dummy
While prev.next.val < reposition_node.val:
prev = prev.next
# Insert the node after the found position
reposition_node.next = prev.next
prev.next = reposition_node
# Return the sorted list, which is dummy.next
Return dummy.next
# Pseudocode Breakdown
Function insertionSortList(head):
If head == null OR head.next == null: # Check for an empty list or single element list
Return head
dummy = new ListNode(0) # Dummy node as a sorted list starting point
dummy.next = head
current = head # Pointer to start traversing the list
While current != null AND current.next != null: # Continue until reaching the end of the list
If current.val <= current.next.val: # If the current node is in correct order
current = current.next # Move to the next node
Else: # Node needs to be repositioned
reposition_node = current.next # Node to be repositioned
current.next = reposition_node.next # Remove it from the list
prev = dummy # Pointer to find the correct position in the sorted part
While prev.next.val < reposition_node.val: # Find where to insert
prev = prev.next # Move prev to next node
reposition_node.next = prev.next # Link reposition_node to the sorted part
prev.next = reposition_node # Insert the node after prev
Return dummy.next # Return the sorted list
This detailed approach ensures every step is clear and logically follows the insertion sort paradigm, applied specifically to singly linked lists. The dummy node simplifies edge cases and maintains a clean structure.