Skip to main content

Merge Sort Doubly Linked List

MediumPremium

You are given the head of a doubly linked list. Write a function to sort the linked list in either ascending or descending order using merge sort.

Constraints

  • You must implement the sorting algorithm using the merge sort technique.
  • The sorting should be performed in-place, i.e., do not create a new list.
  • You can assume the input list is non-empty.

Example

input: Doubly Linked List [1, 3, 2], ascending output: [1, 2, 3] input: Doubly Linked List [1, 3, 2], descending output: [3, 2, 1]