Advertisement

Insertion Sort List - LeetCode 147 Solution

Insertion Sort List - Complete Solution Guide

Insertion Sort List is LeetCode problem 147, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the head of a singly linked list, sort the list using insertion sort , and return the sorted list's head . The steps of the insertion sort algorithm: Insertion sort iterates, consuming one input element each repetition and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there. It repeats until no input elements remain. The following is a graphical example of the inse

Detailed Explanation

The problem requires sorting a singly linked list using the insertion sort algorithm. Insertion sort works by iteratively building a sorted sublist. For each element in the unsorted part of the list, we find its correct position within the sorted sublist and insert it there. The goal is to return the head of the fully sorted linked list.

Solution Approach

The solution implements insertion sort on a linked list. It uses a dummy node to represent the head of the sorted portion, which simplifies insertion operations, especially at the beginning. The algorithm iterates through the original list, taking each node and inserting it into the correct position within the sorted portion (starting from the dummy node). It does this by traversing the sorted portion until it finds the appropriate insertion point and then updates the `next` pointers accordingly.

Step-by-Step Algorithm

  1. Step 1: Create a dummy node. This node's `next` pointer will point to the head of the sorted list (which is initially empty).
  2. Step 2: Initialize `curr` to the head of the input linked list. This will be the node we want to insert into the sorted portion.
  3. Step 3: While `curr` is not null, iterate through the unsorted portion of the list.
  4. Step 4: For each `curr` node, find the correct position to insert it into the sorted portion. Start from `dummy` and iterate `prev` until `prev.next` is null or `prev.next.val` is greater than or equal to `curr.val`. This means `curr` should be inserted before `prev.next`.
  5. Step 5: Save the `next` node of `curr` as `next_node` before modifying `curr`'s pointers. This preserves the unsorted part of the list.
  6. Step 6: Insert `curr` between `prev` and `prev.next`: `curr.next = prev.next` and `prev.next = curr`.
  7. Step 7: Move `curr` to the next node in the unsorted part of the list: `curr = next_node`.
  8. Step 8: After iterating through all nodes in the original list, the linked list starting from `dummy.next` will be the sorted list. Return `dummy.next`.

Key Insights

  • Insight 1: Using a dummy node simplifies the insertion process at the beginning of the sorted list, avoiding special case handling for inserting at the head.
  • Insight 2: The core of insertion sort involves finding the correct insertion point in the already sorted portion of the list for each element from the unsorted portion.
  • Insight 3: Iterating and modifying linked list pointers requires careful management to avoid breaking the list and losing track of nodes.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(1)

Topics

This problem involves: Linked List, Sorting.

Companies

Asked at: Google.