Linked List Algorithm Implementations

Swapping Nodes in PairsApproach: Using a dummy head nodeLogic: Create a dummy head node to simplify the swapping process. Use a current pointer that moves forward two steps at a time. The loop continues as long as there are at least two more nodes to swap.Implementation:/** * Definition for singly-linked list. * struct ListNode { * int v ...

Posted on Sat, 09 May 2026 03:30:26 +0000 by drorshem

Minimizing Interval Length Difference for Common Intersection Using Segment Trees

Given $n$ closed intervals on a number line, the objective is to select exactly $m$ intervals such that they share at least one common coordinate point. The cost of a selection is defined as the difference between the maximum length and the minimum length among the chosen intervals. The length of an interval $[l, r]$ is calculated as $r - l$. T ...

Posted on Fri, 08 May 2026 22:58:06 +0000 by Jak-S

Linked List Problems: Swap Pairs, Remove Nth From End, Intersection, and Cycle Detection

24. Swap Nodes in Pairs Problem: Swap adjacent nodes in a linked list pairwise, returnnig the new head. Do not modify node values—only rewire nodes. Approaches: Iterative: Use a dummy head to track the previous node. Adjust pointers for each pair. Recursive: Swap the first two nodes, then recurce on the reamining list. class Solution: ...

Posted on Fri, 08 May 2026 00:18:32 +0000 by dey.souvik007