Solution By Dare2Solve | Detailed Explanation | Clean Code
Explanation []\nauthorslog.com/blog/cRT2Uc7vvh\n\n# Code\n\ncpp []\nclass Solution {\npublic:\n    ListNode* insertionSortList(ListNode* head) {\n        if (!h
```Explanation []\nauthorslog.com/blog/cRT2Uc7vvh\n```\n# Code\n\n```cpp []\nclass Solution {\npublic:\n    ListNode* insertionSortList(ListNode* head) {\n        if (!head)\n            return nullptr;\n\n        std::vector<int> arr;\n        ListNode* current = head;\n\n        while (current) {\n            arr.push_back(current->val);\n            current = current->next;\n        }\n\n        for (int i = 1; i < arr.size(); i++) {\n            int currVal = arr[i];\n            int j = i - 1;\n            while (j >= 0 && arr[j] > currVal) {\n                arr[j + 1] = arr[j];\n                j--;\n            }\n            arr[j + 1] = currVal;\n        }\n\n        current = head;\n        for (int i = 0; i < arr.size(); i++) {\n            current->val = arr[i];\n            current = current->next;\n        }\n\n        return head;\n    }\n};\n```\n\n```python []\nclass Solution:\n    def insertionSortList(self, head: ListNode) -> ListNode:\n        if not head:\n            return None\n\n        arr = []\n        current = head\n\n        while current:\n            arr.append(current.val)\n            current = current.next\n\n        for i in range(1, len(arr)):\n            curr_val = arr[i]\n            j = i - 1\n            while j >= 0 and arr[j] > curr_val:\n                arr[j + 1] = arr[j]\n                j -= 1\n            arr[j + 1] = curr_val\n\n        current = head\n        for i in range(len(arr)):\n            current.val = arr[i]\n            current = current.next\n\n        return head\n\n```\n\n```java []\nclass Solution {\n    public ListNode insertionSortList(ListNode head) {\n        if (head == null) return null;\n\n        List<Integer> arr = new ArrayList<>();\n        ListNode current = head;\n\n        while (current != null) {\n            arr.add(current.val);\n            current = current.next;\n        }\n\n        for (int i = 1; i < arr.size(); i++) {\n            int currVal = arr.get(i);\n            int j = i - 1;\n            while (j >= 0 && arr.get(j) > currVal) {\n                arr.set(j + 1, arr.get(j));\n                j--;\n            }\n            arr.set(j + 1, currVal);\n        }\n\n        current = head;\n        for (int i = 0; i < arr.size(); i++) {\n            current.val = arr.get(i);\n            current = current.next;\n        }\n\n        return head;\n    }\n}\n```\n\n```javascript []\nvar insertionSortList = function (head) {\n    let current = head;\n    const arr = [];\n    while (current) {\n        arr.push(current.val);\n        current = current.next;\n    }\n\n    for (let i = 1; i < arr.length; i++) {\n        let currVal = arr[i];\n        let j = i - 1\n        for (; j >= 0 && arr[j] > currVal; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[j + 1] = currVal;\n    }\n\n    current = head;\n    for (let i = 0; i < arr.length; i++) {\n        current.val = arr[i];\n        current = current.next;\n    }\n\n    return head\n};\n```
['Linked List', 'Sorting', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']