<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Giuseppe</title>
    <description>The latest articles on DEV Community by Giuseppe (@desiato).</description>
    <link>https://dev.to/desiato</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3138861%2Ff625adde-c046-4b8c-ab03-ff34c54153b8.jpg</url>
      <title>DEV Community: Giuseppe</title>
      <link>https://dev.to/desiato</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/desiato"/>
    <language>en</language>
    <item>
      <title>LeetCode #238. Product of Array Except Self</title>
      <dc:creator>Giuseppe</dc:creator>
      <pubDate>Sat, 07 Feb 2026 14:40:38 +0000</pubDate>
      <link>https://dev.to/desiato/leetcode-238-product-of-array-except-self-5355</link>
      <guid>https://dev.to/desiato/leetcode-238-product-of-array-except-self-5355</guid>
      <description>&lt;h1&gt;
  
  
  1.Solution with division
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Time Complexity O(n)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;First loop iterates through all n elements once to calculate the product and count zeros&lt;/li&gt;
&lt;li&gt;Second loop iterates through all n elements once to populate the result array&lt;/li&gt;
&lt;li&gt;Total: O(n) + O(n) = O(n)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Space Complexity
&lt;/h2&gt;

&lt;p&gt;O(1) auxiliary space: Only uses a constant amount of extra variables (zeroCount, product, i).&lt;br&gt;
The space complexity is typically considered O(1) since the output array doesn't count as extra space (it's required by the problem), and no additional space proportional to input size is used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] result = new int[n];

        int zeroCount = 0;
        int product = 1;

        for (int num : nums) {
            if (num == 0)
                zeroCount++;
            else 
                product *= num;
        }

        for (int i = 0; i &amp;lt; n; i++) {
            if (zeroCount &amp;gt; 1)
                result[i] = 0;
            if (zeroCount == 1)
                result[i] = (nums[i] == 0) ? product : 0;
            else 
                result[i] = product / nums[i];
        }

        return result;
    }
}  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  2.Solution without division
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Time Complexity: O(n)
&lt;/h2&gt;

&lt;p&gt;You do two linear passes over the array:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prefix pass (left → right)&lt;/li&gt;
&lt;li&gt;Suffix pass (right → left)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each element is processed a constant number of times.&lt;br&gt;
O(n) + O(n) = O(n)&lt;/p&gt;

&lt;h1&gt;
  
  
  Space Complexity: O(1)
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int[] productExceptSelf(int[] nums) {
        int[] result = new int[nums.length];

        // prefix product
        result[0] = 1;
        for (int i = 1; i &amp;lt;nums.length; i++) {
            result[i] = result[i - 1] * nums[i - 1]; 
        }

        // suffix product
        int suffix = 1;
        for (int i = nums.length - 1; i &amp;gt;= 0; i--) {
            result[i] *= suffix;
            suffix *= nums[i];
        }
        return result;    
    }
}  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>LeetCode #271. Encode and Decode Strings</title>
      <dc:creator>Giuseppe</dc:creator>
      <pubDate>Sat, 31 Jan 2026 16:56:28 +0000</pubDate>
      <link>https://dev.to/desiato/leetcode-271-encode-and-decode-strings-33an</link>
      <guid>https://dev.to/desiato/leetcode-271-encode-and-decode-strings-33an</guid>
      <description>&lt;h3&gt;
  
  
  Time Complexity: O(n)
&lt;/h3&gt;

&lt;p&gt;for encoding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each character of each string is appended once&lt;/li&gt;
&lt;li&gt;StringBuilder.append() is amortized O(1) per char&lt;/li&gt;
&lt;li&gt;Total work = proportional to total characters&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;for decoding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pointer i only moves forward&lt;/li&gt;
&lt;li&gt;Each character is visited a constant number of times&lt;/li&gt;
&lt;li&gt;No backtracking or nested re-scanning&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Space Complexity: O(n)
&lt;/h3&gt;

&lt;p&gt;You must store:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;encoded string (encode)&lt;/li&gt;
&lt;li&gt;decoded list of strings (decode)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s O(n) by necessity&lt;/p&gt;

&lt;p&gt;Extra (auxiliary) space&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only a few integers and pointers → O(1)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {

    // Encodes a list of strings to a single string.
    public String encode(List&amp;lt;String&amp;gt; strs) {
        StringBuilder sb = new StringBuilder();

        for (String s : strs) {
            sb.append(s.length())
              .append("#")
              .append(s);
        }
        return sb.toString();
    }

    // Decodes a single string to a list of strings.
    public List&amp;lt;String&amp;gt; decode(String s) {
        //create a list of String to return tthe result
        List&amp;lt;String&amp;gt; result = new ArrayList&amp;lt;&amp;gt;();
        // overall string pointer
        int i = 0;

        while (i &amp;lt; s.length()) {
            // pointer to find the delimiter '#'
            int j = i;
            while (s.charAt(j) != '#') {
                j++;
            }

            // parse length
            int length = Integer.parseInt(s.substring(i, j));

            // extract string
            String str = s.substring(j + 1, j + 1 + length);
            result.add(str);

            // move pointer
            i = j + 1 + length;
        }

        return result;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>LeetCode #347. Top K Frequent Element</title>
      <dc:creator>Giuseppe</dc:creator>
      <pubDate>Sat, 31 Jan 2026 09:49:38 +0000</pubDate>
      <link>https://dev.to/desiato/leetcode-347-top-k-frequent-element-1dni</link>
      <guid>https://dev.to/desiato/leetcode-347-top-k-frequent-element-1dni</guid>
      <description>&lt;h3&gt;
  
  
  Time Complexity: O(n log n) dominated by sorting
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Space Complexity: O(n) for the HashMap and List
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int[] topKFrequent(int[] nums, int k) {
        Map&amp;lt;Integer, Integer&amp;gt; map = new HashMap&amp;lt;&amp;gt;();
        for (int num : nums) {
            map.put(num, map.getOrDefault(num, 0) +1);
        }

        List&amp;lt;Integer&amp;gt; list = new ArrayList&amp;lt;&amp;gt;(map.keySet());
        list.sort((a,b) -&amp;gt; Integer.compare(map.get(b), map.get(a)));

        int[] result = new int[k];
        for (int i = 0; i &amp;lt; k; i++){
            result[i] = list.get(i);
        }
        return result;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>LeetCode #647. Palindromic Substrings</title>
      <dc:creator>Giuseppe</dc:creator>
      <pubDate>Tue, 02 Dec 2025 09:53:37 +0000</pubDate>
      <link>https://dev.to/desiato/leetcode-647-palindromic-substrings-4835</link>
      <guid>https://dev.to/desiato/leetcode-647-palindromic-substrings-4835</guid>
      <description>&lt;h1&gt;
  
  
  Brute force solution
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Time Complexity: 0(n^3)
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Space Complexity: O(1)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int countSubstrings(String s) {
        int count = 0;

        for (int i = 0; i &amp;lt; s.length(); i++) {
            for (int j = i; j &amp;lt; s.length(); j++){
                if (isPalindrome(s, i, j)) {
                    count++;
                }
            }
        }
        return count;

    }

    public boolean isPalindrome(String s, int start, int end) {
        while (start &amp;lt; end) {
            if (s.charAt(start) != s.charAt(end)) return false;

            start++;
            end--;
        }
        return true;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>LeetCode #98.Validate Binary Search Tree</title>
      <dc:creator>Giuseppe</dc:creator>
      <pubDate>Wed, 26 Nov 2025 11:54:53 +0000</pubDate>
      <link>https://dev.to/desiato/leetcode-98validate-binary-search-tree-56ee</link>
      <guid>https://dev.to/desiato/leetcode-98validate-binary-search-tree-56ee</guid>
      <description>&lt;h2&gt;
  
  
  Time Complexity: O(n)
&lt;/h2&gt;

&lt;p&gt;We visit every node exactly once&lt;br&gt;
At each node, we do constant work (a few comparisons)&lt;br&gt;
So total work = O(n) where n = number of nodes&lt;/p&gt;
&lt;h2&gt;
  
  
  Space Complexity: O(h) → where h = height of the tree
&lt;/h2&gt;

&lt;p&gt;This comes from the recursion stack (call stack).&lt;br&gt;
Best case: O(log n)&lt;br&gt;
→ Balanced tree (height ≈ log n)&lt;br&gt;
→ Maximum stack depth = log n&lt;br&gt;
Worst case: O(n)&lt;br&gt;
→ Completely skewed tree (like a linked list)&lt;br&gt;
→ Stack depth = n (all nodes on one side)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public boolean isValidBST(TreeNode root) {
        return helper(root, null, null);
    }

    private boolean helper(TreeNode node, Integer lower, Integer upper) {
        // Empty tree is valid
        if (node == null) {
            return true;
        }

        // Check bounds
        if (lower != null &amp;amp;&amp;amp; node.val &amp;lt;= lower) return false;
        if (upper != null &amp;amp;&amp;amp; node.val &amp;gt;= upper) return false;

        // Left subtree: all values must be &amp;lt; node.val  → tighten the upper bound
        // Right subtree: all values must be &amp;gt; node.val → tighten the lower bound
        if (!helper(node.left, lower, node.val)) return false;
        if (!helper(node.right, node.val, upper)) return false;

        return true;

    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>LeetCode #104. Maximum Depth of Binary Tree</title>
      <dc:creator>Giuseppe</dc:creator>
      <pubDate>Wed, 19 Nov 2025 15:15:25 +0000</pubDate>
      <link>https://dev.to/desiato/leetcode-104-maximum-depth-of-binary-tree-2f44</link>
      <guid>https://dev.to/desiato/leetcode-104-maximum-depth-of-binary-tree-2f44</guid>
      <description>&lt;h2&gt;
  
  
  Time Complexity: O(n)
&lt;/h2&gt;

&lt;p&gt;The function visits every node in the tree exactly once, where n is the total number of nodes. Each visit performs constant-time operations (comparison and addition), so the overall time complexity is linear.&lt;/p&gt;

&lt;h2&gt;
  
  
  Space Complexity: O(h)
&lt;/h2&gt;

&lt;p&gt;h = height of the tree (maximum depth)&lt;br&gt;
Best case (balanced tree): O(log n)&lt;/p&gt;

&lt;p&gt;A perfectly balanced tree has height ≈ log₂(n).&lt;br&gt;
So the deepest recursion chain is log(n) → stack size O(log n).&lt;/p&gt;

&lt;p&gt;Worst case (skewed tree): O(n)&lt;/p&gt;

&lt;p&gt;If each node only has one child (linked-list shape):&lt;br&gt;
height = n&lt;br&gt;
→ recursion stack size = n&lt;br&gt;
→ O(n).&lt;/p&gt;

&lt;p&gt;Average case: O(log n)&lt;/p&gt;

&lt;p&gt;Random/typical trees tend to be reasonably balanced, so expected height ≈ log n.&lt;/p&gt;

&lt;p&gt;Classic recursive problem because the depth of a tree is naturally defined in terms of the depths of its subtrees.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int leftDepth = maxDepth(root.left);
        int rightDepth = maxDepth(root.right);

        return Math.max(leftDepth, rightDepth) + 1;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We recursively calculate the maximum depth of the left subtree and the right subtree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As long as a node has children, the recursive calls continue deeper into the tree, and the current function cannot execute its return statement yet because it is waiting for the recursive results.&lt;/p&gt;

&lt;p&gt;Once we reach a leaf node, its .left and .right are null, so the recursive calls hit the base case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (root == null) return 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, the recursion starts returning upward (unwinding the call stack), and each node can finally execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return Math.max(leftDepth, rightDepth) + 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So:&lt;br&gt;
Recursion goes down until null, then the return statements execute while coming back up.&lt;/p&gt;

</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>LeetCode #19. Remove nth Node from End of List</title>
      <dc:creator>Giuseppe</dc:creator>
      <pubDate>Wed, 19 Nov 2025 10:46:36 +0000</pubDate>
      <link>https://dev.to/desiato/leetcode-19-remove-nth-node-from-end-of-list-59bg</link>
      <guid>https://dev.to/desiato/leetcode-19-remove-nth-node-from-end-of-list-59bg</guid>
      <description>&lt;h2&gt;
  
  
  Space Complexity: O(1)
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Time Complexity: O(n)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null || head.next == null) {
            return null;
        }

        ListNode dummy = new ListNode();
        dummy.next = head;

        ListNode slow = dummy;
        ListNode fast = dummy;

        // advance fast node till the nth node
        for (int i = 0; i &amp;lt;= n; i++) {
            fast = fast.next;
        }

        // advance both nodes till fast != null
        while (fast != null) {
            slow = slow.next;
            fast = fast.next;
        }

        // slow now is at the node before the node that has to be deleted
        // bypass the nth node that be to be deleted
        // so we just do not reference that node anymore
        slow.next = slow.next.next;

        return dummy.next;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>LeetCode #141. Linked List Cycle</title>
      <dc:creator>Giuseppe</dc:creator>
      <pubDate>Mon, 10 Nov 2025 11:39:54 +0000</pubDate>
      <link>https://dev.to/desiato/leetcode-141-linked-list-cycle-1h47</link>
      <guid>https://dev.to/desiato/leetcode-141-linked-list-cycle-1h47</guid>
      <description>&lt;h2&gt;
  
  
  Time Complexity: O(n)
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Space Complexity: O(1)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Solution {
    public boolean hasCycle(ListNode head) {

         if (head == null || head.next == null) {
            return false;
        }

        // Initialize two pointers
        ListNode slow = head;
        ListNode fast = head;

        // Move pointers until they meet or fast reaches end
        while (fast != null &amp;amp;&amp;amp; fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;

            // If pointers meet, there's a cycle
            if (slow == fast) {
                return true;
            }
        }

        // Fast pointer reached end, no cycle
        return false;

    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>LeetCode #234. Palindrome Linked List</title>
      <dc:creator>Giuseppe</dc:creator>
      <pubDate>Fri, 07 Nov 2025 11:39:06 +0000</pubDate>
      <link>https://dev.to/desiato/leetcode-234-palindrome-linked-list-1cmg</link>
      <guid>https://dev.to/desiato/leetcode-234-palindrome-linked-list-1cmg</guid>
      <description>&lt;h2&gt;
  
  
  Time Complexity: O(n)
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Space Complexity: O(1)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public boolean isPalindrome(ListNode head) {
        // trivially palindrome
        if (head == null || head.next == null) return true;

        // Find middle
        ListNode slow = head;
        ListNode fast = head;

        while (fast != null &amp;amp;&amp;amp; fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        // If odd length, skip the middle node
        // because in the odd case the middle node is not relevant for nowing if a list is palindrome
        if (fast != null) slow = slow.next;

        // Reverse second half
        ListNode second = reverse(slow);

        // Compare halves
        ListNode p1 = head;
        ListNode p2 = second;
        boolean ok = true;
        while (p2 != null) {
            if (p1.val != p2.val) { 
                ok = false; 
                break; 
            }
            p1 = p1.next;
            p2 = p2.next;
        }

        return ok;
    }

    private ListNode reverse(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;

        while (curr != null) {
            ListNode nxt = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nxt;
        }
        return prev;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>LeetCode #2. Add Two Numbers</title>
      <dc:creator>Giuseppe</dc:creator>
      <pubDate>Thu, 30 Oct 2025 08:19:49 +0000</pubDate>
      <link>https://dev.to/desiato/leetcode-2-add-two-numbers-54f8</link>
      <guid>https://dev.to/desiato/leetcode-2-add-two-numbers-54f8</guid>
      <description>&lt;h2&gt;
  
  
  Time Complexity: O(n)
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Space Complexity: O(1)
&lt;/h2&gt;

&lt;p&gt;Total space (including result list): O(n)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 

        ListNode dummy = new ListNode();
        ListNode current = dummy;
        int carry = 0;

        // Continue looping as long as there’s at least one more digit to process or a carry to handle.
        while (l1 != null || l2 != null || carry != 0) {
            // extract the values (if node exists, else 0)
            int val1 = (l1 != null) ? l1.val : 0;
            int val2 = (l2 != null) ? l2.val : 0;

            // compute total sum with carry
            int sum = val1 + val2 + carry;

            // determine next carry and current digit
            carry = sum / 10;
            int digit = sum % 10;

            // create new node with the digit and attach
            current.next = new ListNode(digit);
            current = current.next;

            // move input pointers forward
            if (l1 != null) l1 = l1.next;
            if (l2 != null) l2 = l2.next;

        }  

        return dummy.next;   
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>LeetCode #206. Reverse Linked List</title>
      <dc:creator>Giuseppe</dc:creator>
      <pubDate>Wed, 29 Oct 2025 11:25:49 +0000</pubDate>
      <link>https://dev.to/desiato/leetcode-206-reverse-linked-list-31l0</link>
      <guid>https://dev.to/desiato/leetcode-206-reverse-linked-list-31l0</guid>
      <description>&lt;h2&gt;
  
  
  Time Complexity: O(n)
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Space Complexity: O(1)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public ListNode reverseList(ListNode head) {

        // these variables are references, not copies of the node itself
        // so we are not using them as copies of the nodes, but as references (pointers)
        ListNode previous = null;
        ListNode current = head;
        ListNode next = null;

        while (current != null) {

            next = current.next; // save next
            current.next = previous; // reverse

            // advance previous &amp;amp; current
            previous = current; // previous points to the old current
            current = next; // current points to the old next
        }

        return previous; // new head at the end

    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>LeetCode #237. Delete node in a LinkedList.</title>
      <dc:creator>Giuseppe</dc:creator>
      <pubDate>Mon, 27 Oct 2025 11:54:39 +0000</pubDate>
      <link>https://dev.to/desiato/leetcode-237-delete-node-in-a-linkedlist-2hpk</link>
      <guid>https://dev.to/desiato/leetcode-237-delete-node-in-a-linkedlist-2hpk</guid>
      <description>&lt;h2&gt;
  
  
  Time Complexity: O(1)
&lt;/h2&gt;

&lt;p&gt;Copy the value from the next node: node.val = node.next.val&lt;br&gt;
Update the pointer to skip the next node: node.next = node.next.next&lt;/p&gt;

&lt;p&gt;No loops, no recursion, no operations that depend on the size of the linked list. It always takes the same amount of time.&lt;/p&gt;

&lt;p&gt;We're not actually deleting the node we were given. Instead, we're:&lt;/p&gt;

&lt;p&gt;Overwriting its value with the next node's value&lt;br&gt;
Bypassing the next node by updating the pointer to skip over it&lt;/p&gt;

&lt;p&gt;So if we have: 1 -&amp;gt; [2] -&amp;gt; 3 -&amp;gt; 4 (delete node with value 2)&lt;br&gt;
After the operation: 1 -&amp;gt; [3] -&amp;gt; 4&lt;br&gt;
The node object at the second position still exists in memory at the same location, but now it:&lt;/p&gt;

&lt;p&gt;Contains the value 3 (copied from the next node)&lt;br&gt;
Points directly to the node with value 4&lt;/p&gt;

&lt;p&gt;The original "node 3" is now orphaned (no references to it), so it becomes eligible for garbage collection.&lt;/p&gt;

&lt;h2&gt;
  
  
  Space Complexity: O(1)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public void deleteNode(ListNode node) {

        node.val = node.next.val;
        node.next = node.next.next;

    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
    </item>
  </channel>
</rss>
