# LeetCode: Sort List Solution

Yeah I skipped the follow-up

## Approach: my same ol' cheat

Convert the list into an array

Sort the array

Rebuild the array into a linked list

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}var sortList = function (head) {2  if (!head) return head3
4  let arr = []5  for (; head; head = head.next) {6    arr.push(head.val)7  }8  arr.sort((a, b) => a - b)9
10  arr = arr.map(val => new ListNode(val))11  for (let i = 0; i < arr.length - 1; i++) {12    arr[i].next = arr[i + 1]13  }14
15  return arr[0]16}```

Original problem

## Similar problems

Merge Two Sorted Lists

Sort Colors

Insertion Sort List

Sort Linked List Already Sorted Using Absolute Values

## Comments

Loading comments...

leetcode

array

sorting

linked list

## Next Post

LeetCode: Longest Palindrome by Concatenating Two Letter Words

Aug 30, 2022

Things get messy sometimes

## Previous Post

LeetCode: Add Strings

Aug 28, 2022

Add digits with carry

Search Posts