Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Linked List Sorting

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Linked List Sorting. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Prime Video stores a processing queue as a singly linked list of integer priorities. Sort the list in ascending order and return the sorted head, reusing the existing nodes rather than creating an array of values.

Formal Specification

Implement sort_list(head), where head is either None or the first node of a singly linked list. Each node has an integer val field and a next field containing the next node or None. Return the head of the same list after sorting. Duplicate values must be preserved, and the algorithm must rearrange node links in place.

The expected output in the examples and test cases is shown as an array of node values from the returned list. The judge converts that array representation to and from linked-list nodes.

Constraints

  • 0 <= n <= 5 * 10^4
  • -10^5 <= node.val <= 10^5
  • The input is a singly linked list
  • The returned list must reuse the original nodes

Function Signature

def sort_list(head):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output