147. Insertion Sort List

Dare2Solve

Dare2Solve

147. Insertion Sort List
SAMSUNG 49-Inch Odyssey G9
SAMSUNG 49-Inch Odyssey G9
Because earth is not flat

Description

This problem involves [describe the problem context, e.g., working with a specific data structure or solving a particular type of problem]. The goal is to [explain the main objective of the problem]. The problem tests the understanding of [mention any specific concepts, e.g., algorithms, data structures] and how they can be applied to achieve the desired solution.

Intuition

The intuition behind solving this problem lies in [explain the key idea or insight needed to solve the problem]. By understanding [mention any underlying principle or observation], we can approach the problem more effectively. The key is to [describe what the solver needs to focus on or the main strategy they should employ].

Approach

  1. [Step 1]:
    • [Describe the first step in the approach, explaining what needs to be done and why it's important].
  2. [Step 2]:
    • [Detail the next step, breaking down how to implement it and any considerations to keep in mind].
  3. [Subsequent Steps]:
    • [Continue to describe the subsequent steps, ensuring each part of the approach is clear and logical].
  4. [Final Step]:
    • [Conclude with the final step, which should tie the solution together and lead to the desired outcome].

Complexity

Time Complexity:

The time complexity of this solution is (O([explain time complexity])), where (n) is [describe what (n) represents, e.g., the number of elements, size of input, etc.]. This is because [give a brief explanation of why this is the time complexity].

Space Complexity:

The space complexity is (O([explain space complexity])), as [explain why this amount of space is needed, considering any auxiliary space used].

Code

C++

class Solution {
public:
    ListNode* insertionSortList(ListNode* head) {
        if (!head)
            return nullptr;

        std::vector<int> arr;
        ListNode* current = head;

        while (current) {
            arr.push_back(current->val);
            current = current->next;
        }

        for (int i = 1; i < arr.size(); i++) {
            int currVal = arr[i];
            int j = i - 1;
            while (j >= 0 && arr[j] > currVal) {
                arr[j + 1] = arr[j];
                j--;
            }
            arr[j + 1] = currVal;
        }

        current = head;
        for (int i = 0; i < arr.size(); i++) {
            current->val = arr[i];
            current = current->next;
        }

        return head;
    }
};

Python

class Solution:
    def insertionSortList(self, head: ListNode) -> ListNode:
        if not head:
            return None

        arr = []
        current = head

        while current:
            arr.append(current.val)
            current = current.next

        for i in range(1, len(arr)):
            curr_val = arr[i]
            j = i - 1
            while j >= 0 and arr[j] > curr_val:
                arr[j + 1] = arr[j]
                j -= 1
            arr[j + 1] = curr_val

        current = head
        for i in range(len(arr)):
            current.val = arr[i]
            current = current.next

        return head

Java

class Solution {
    public ListNode insertionSortList(ListNode head) {
        if (head == null) return null;

        List<Integer> arr = new ArrayList<>();
        ListNode current = head;

        while (current != null) {
            arr.add(current.val);
            current = current.next;
        }

        for (int i = 1; i < arr.size(); i++) {
            int currVal = arr.get(i);
            int j = i - 1;
            while (j >= 0 && arr.get(j) > currVal) {
                arr.set(j + 1, arr.get(j));
                j--;
            }
            arr.set(j + 1, currVal);
        }

        current = head;
        for (int i = 0; i < arr.size(); i++) {
            current.val = arr.get(i);
            current = current.next;
        }

        return head;
    }
}

JavaScript

var insertionSortList = function (head) {
    let current = head;
    const arr = [];
    while (current) {
        arr.push(current.val);
        current = current.next;
    }

    for (let i = 1; i < arr.length; i++) {
        let currVal = arr[i];
        let j = i - 1
        for (; j >= 0 && arr[j] > currVal; j--) {
            arr[j + 1] = arr[j];
        }
        arr[j + 1] = currVal;
    }

    current = head;
    for (let i = 0; i < arr.length; i++) {
        current.val = arr[i];
        current = current.next;
    }

    return head
};