Sort List (Merge Sort on Linked Lists)
Divide and Conquer / Slow & Fast PointersSolved Nov 22, 2025
Asked at 9 companies
5 recent
- Amazon
- Bloomberg
- Microsoft
- Meta
- Lyft
- ByteDance
- Oracle
- TikTok
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven the head of a linked list, return the list after sorting it in ascending order. The solution should run in O(n log n) time and using constant extra space (excluding the recursion stack).ReadHide
Problem Description
Given the head of a linked list, return the list after sorting it in ascending order. The solution should run in O(n log n) time and using constant extra space (excluding the recursion stack).
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Linked lists do not support random access, making Merge Sort the ideal choice. Use the 'Slow and Fast Pointers' technique to find the middle of the list, recursively split the list, and then merge the sorted halves.
Approaches
1. Merge Sort (Optimal) - O(N log N) ⭐
Idea: 1. Find Middle: Use a slow and fast pointer to find the middle node to split the list into two halves. 2. Recursive Split: Recursively call sortList on both halves. 3. Merge: Merge the two sorted linked lists using a dummy node and a tail pointer.
2. Bubble Sort / Insertion Sort (Brute Force) - O(N²)
Idea: Repeatedly traverse the list to swap nodes or shift elements. This is highly inefficient for large lists.
Solution code
from typing import Optional
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
# Optimized: O(N log N) Time, O(log N) Space
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
return head
# 1. Split the list into two halves
left = head
right = self.getMid(head)
tmp = right.next
right.next = None
right = tmp
# 2. Recursive Sort
left = self.sortList(left)
right = self.sortList(right)
# 3. Merge
return self.merge(left, right)
def getMid(self, head):
slow, fast = head, head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
def merge(self, list1, list2):
dummy = tail = ListNode()
while list1 and list2:
if list1.val < list2.val:
tail.next = list1
list1 = list1.next
else:
tail.next = list2
list2 = list2.next
tail = tail.next
if list1:
tail.next = list1
if list2:
tail.next = list2
return dummy.nextOther ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language