Linked List Algorithms: Swapping, Removing, Finding Intersections, and Detecting Cycles
Swapping Nodes in Pairs
Problem: Given a linked list, swap every two adjacent nodes and return the modified list. You must not modify the values in the nodes, only the nodes themselves.
The key approach involves careful pointer manipulation and the use of a temporray node to preserve references.
We'll use a dummy node to simplify the edge ca ...
Posted on Mon, 10 Aug 2026 16:32:31 +0000 by MoombaDS
Linked List Operations: Swapping Nodes, Removing Nth Node, Finding Intersections, and Detecting Cycles
Pairwise Node Swapping
To swap adjacent nodes in pairs, we utilize a dummy node to simplify edge cases. The core idea involves manipulating pointers to reverse each pair while maintaining proper linkage with the rest of the list. A cursor pointer tracks the predecessor of each pair being processed.
The termination condition varies based on whet ...
Posted on Sat, 20 Jun 2026 16:35:52 +0000 by matt6805
Hash-Based Algorithms for String and Array Problems
1. Valid Anagram
Given two strings s and t, determine if they are anagrams — i.e., contain the exact same characters with the same frequencies.
Since characters are constrained to lowercase English letters, a fixed-size integer array of length 26 suffices for counting frequencies.
public boolean checkAnagram(String text1, String text2) {
i ...
Posted on Fri, 12 Jun 2026 16:26:39 +0000 by FillePille
Linked List Algorithms: Pair Swapping, Node Removal, Intersection Detection, and Cycle Identification
24. Swap Nodes in Pairs
Problem Link: LeetCode 24
Solution:
This problem involves swapping adjacent nodes in a linked list in pairs. A dummy head node simplifies edge case handling, such as when the list has only one or two nodes.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {}
};
class Soluti ...
Posted on Tue, 19 May 2026 01:54:15 +0000 by Buttero