Merge Sort Implementation on Linked Lists
Sorting a Linked List Using Divide-and-Conquer
To sort a singly linked list efficiently, we can apply the merge sort algorithm which ensures O(n log n) time complexity. There are two primary approaches: top-down (recursive) and bottom-up (iterative). Below is an implementation of the recursive variant.
class Solution {
public ListNode sortL ...
Posted on Fri, 25 Sep 2026 16:20:16 +0000 by kharbat
LeetCode Problem Solutions: Linked List Sorting and Interval Merging
148. Sort List
Problem Statement
Given the head of a linked list, sort the list in ascending order and return the sorted list.
Approach
For this problem, we can implement a merge sort algorithm with O(1) space complexity by using a bottom-up approach. The key steps involve:
Determining the length of the linked list
Splitting the list into subl ...
Posted on Mon, 31 Aug 2026 16:24:28 +0000 by unistake
Merge Sort Implementation for Singly Linked Lists
Algorithm Overview
Split: Use slow-fast pointer technique to locate the midpoint and partition the list into two halves.
Recurse: Apply the same sorting procedure recursively on both halves.
Merge: Combine the two sorted sublists into a single sorted list using a linear-time merge step.
Implementation
class ListNode {
int value;
ListN ...
Posted on Fri, 19 Jun 2026 17:24:15 +0000 by brainstem
Counting Array Inversions Efficiently
Problem Specification
Given a sequence of n integers, compute the total number of inversions contained within the array. An inversion is formally defined as a pair of indices (i, j) satisfying i < j and A[i] > A[j].
Constraints & Limits
Sequence length: 1 ≤ n ≤ 10<sup>5</sup>
Element values: 0 ≤ A[i] ≤ 10<sup>9</ ...
Posted on Sat, 23 May 2026 18:36:57 +0000 by garydt