"Day 80: Remove Linked List Elements in Java"

🔥 Day 80 of my 100 Days of Code Problem: Remove Linked List Elements (LeetCode #203) Problem Statement (Simplified): Remove all nodes of a linked list that have a given value. Code : class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummy = new ListNode(0); // Dummy node dummy.next = head; ListNode current = dummy; while (current.next != null) { if (current.next.val == val) { current.next = current.next.next; // Skip the node } else { current = current.next; // Move forward } } return dummy.next; } } Time Complexity: O(n) traverse the list once. Space Complexity: O(1) constant extra space (just dummy node). #Day80 #LeetCode #100DaysOfCode #Java

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories