Linked List Problems: Swapping, Removal, Intersection, and Cycle Detection

Swapping Nodes in Pairs Problem: Given a linked list, swap every two adjacent nodes and return its head. Iterative Approach: class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } class Solution { public ListNode swapPairs(ListNode head) { if (head == null || head.next == null) { ...

Posted on Sat, 09 May 2026 04:52:05 +0000 by psurrena

Dynamic Programming Solutions for LeetCode 343 and 96

LeetCode 343: Integer Break Given an integer n, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers. Return the maximum product you can get. Dynamic Programmign Approach The problem can be solved using dynamic programming. The key insight is that the maximum product for a number i depends on ...

Posted on Sat, 09 May 2026 02:17:40 +0000 by Svoboda

Java Solutions for LeetCode Linked List Problems Following Code Record

Remove Linked List Elements (LeetCode 203) Given the head of a linked list and an integer val, remove all nodes where Node.val == val and return the new head node. Using a dummy head simplifies handling edge cases, especially when the head node itself needs to be removed. A traversal pointer prev is used to point to the node preceding the one c ...

Posted on Fri, 08 May 2026 12:36:38 +0000 by Anti-Moronic

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

Efficient Solutions for Word Search II Problem

Given an m x n board of characters and a list of strings words, return all words on the board. Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word. 1. Trie with DFS Build a trie from the given wor ...

Posted on Thu, 07 May 2026 19:06:04 +0000 by Lassie

Calculating the Length of the Longest Substring Without Repeating Characters

Given a string, identify the length of its longest contiguous substring containing no duplicate cahracters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The longest substring without repeating characters is "abc", which has a length of 3. Example 2: Input: "bbbbb" Output: 1 Explanation: The longest substrin ...

Posted on Thu, 07 May 2026 14:57:56 +0000 by rmbarnes82

Finding the Longest Palindromic Substring Using Dynamic Programming

Given a string text, the objective is to locate longest contiguous substring that reads the same forward and backward. Constraints: 1 <= text.length <= 1000 text consists of alphanumeric English characters only. Dynamic Programming Approahc 1. State Definition Define a 2D table is_palindrome[i][j] where i and j are indices. The value i ...

Posted on Thu, 07 May 2026 10:45:37 +0000 by Pnop