LeetCode 54: Spiral Matrix and 445: Add Two Numbers II

LeetCode 54: Spiral Matrix Problem Given an m x n matrix, return all elements of the matrix in spiral order. Example 1 Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5] Example 2 Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] Output: [1,2,3,4,8,12,11,10,9,5,6,7] Approach Traverse from left to right along the top r ...

Posted on Sun, 14 Jun 2026 16:12:36 +0000 by thebusinesslad

Mastering Linked List Techniques: Pairwise Swapping, Nth Node Removal, Intersection, and Cycle Detection

Problem: 24. Swap Nodes in Pairs To swap two adjacent nodes, we need a pointer standing just before the pair. A dummy sentinel node placed before the head simplifies edge cases. The traversal pointer curr starts at the sentinel. Swapping involves rerouting next pointers in three steps while preserving references that might be lost. The loop con ...

Posted on Tue, 02 Jun 2026 17:52:23 +0000 by deurwaarder

Implementation of a Singly Linked List

#include <bits/stdc++.h> using namespace std; class Node { public: int value; Node* next; }; bool initialize(Node*& list) { list = new Node; if (!list) return false; list->next = nullptr; return true; } bool addAtHead(Node*& list, Node* element) { if (!list || !element) return false; element-& ...

Posted on Sun, 31 May 2026 22:21:19 +0000 by rashpal

Singly Linked List Reversal: Iterative and Recursive Solutions for LeetCode 206

Problem Statement Given the head of a singly linked list, reverse the order of all nodes in the list and return the head of the reversed list. Sample Input 1: head = [1,2,3,4,5] Sample Output 1: [5,4,3,2,1] Sample Input 2: head = [1,2] Sample Output 2: [2,1] Sample Input 3: head = [] Sample Output 3: [] Constraints: The number of nodes i ...

Posted on Sun, 31 May 2026 19:45:10 +0000 by cowboysdude

Linked List Algorithms from Code Thinking Record

Table of Contents Introduction Remove Linked List Elements (LeetCode--203) Design Linked List (LeetCode--707) Reverse Linked List (LeetCode--206) Swap Nodes in Pairs (LeetCode--24) Remove Nth Node From End of List (LeetCode--19) Linked List Cycle II (LeetCode--142) Introduction Following the Code Thinking Record series, this article explores ...

Posted on Sun, 31 May 2026 19:14:58 +0000 by gingerboy101

Linked List Operations: Node Swapping, Removal, Intersection, and Cycle Detection

Pairwise Node Swapping in Linked List Given a linked list, swap every two adjacnet nodes and return the modified list's head. Node values must not be altered; only node positions can be chenged. Example: Input: head = [1,2,3,4] Output: [2,1,4,3] Solution: Use three pointers to manage node connections during swapping. class Solution { publi ...

Posted on Fri, 22 May 2026 21:15:12 +0000 by shenmue232

Core Data Structure Interview Questions and Algorithmic Solutions

Stack and Queue Fundamentals Stacks and queues share the trait that insertion and deletion occur solely at their endpoints. Typical stack storage models are sequential arrays and linked lists. A stack exhibits last-in-first-out behavior. Linked lists lack random access; elemants must be traversed sequentially. Linked representation simplifies ...

Posted on Wed, 20 May 2026 05:56:52 +0000 by Adam W

Finding the Entry Point of a Linked List Cycle Using Floyd's Algorithm

Problem DescriptionGiven the head of a linked list, determine the node where a cycle begins. If no cycle exists, return null. The cycle is identified when a node can be reached again by continuously following the next pointer. The solution must not modify the original linked list.Algorithm ExplanationFloyd's Cycle Detection Algorithm, also know ...

Posted on Mon, 18 May 2026 06:09:36 +0000 by jordy

Implementing a Headed Circular Doubly Linked List in C

Structural DefinitionA headed circular doubly linked list utilizes a sentinel node (head) that acts as a starting point. Unlike a singly linked list, each node contains two pointers: prev pointing to the predecessor and next pointing to the successor. The sentinel node's prev points to the tail, and the tail's next points back to the sentinel, ...

Posted on Sun, 17 May 2026 16:15:51 +0000 by Wolverine68

Common Linked List Algorithm Problems and Solutions

Node Class Definition The following ListNode class serves as the foundation for all examples in this article: import java.util.Arrays; public class ListNode { int data; ListNode next = null; public ListNode(int data) { this.data = data; } public String toString(ListNode node) { int[] values = new int[calcu ...

Posted on Thu, 14 May 2026 11:08:20 +0000 by duclet