Skip to main content

Command Palette

Search for a command to run...

Delete Nodes From Linked List Present in Array

Published
2 min read
Delete Nodes From Linked List Present in Array
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given an array of integers nums and the head of a linked list. Return the head of the modified linked list after removing all nodes from the linked list that have a value that exists in nums.

LeetCode Problem - 3217

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;        // Value stored in the node
 *     ListNode next;  // Reference to the next node in the list
 *     ListNode() {}   // Default constructor
 *     ListNode(int val) { this.val = val; }  // Constructor with value only
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }  // Constructor with value and next node
 * }
 */
class Solution {
    // Method to remove nodes from a linked list if their values are present in the 'nums' array
    public ListNode modifiedList(int[] nums, ListNode head) {
        // Set to store unique values from the 'nums' array
        Set<Integer> uniqueNodeValue = new HashSet<>();

        // Add each value from 'nums' to the set
        for (int num : nums) {
            uniqueNodeValue.add(num);
        }

        // Create a dummy node to simplify the construction of the new list
        ListNode dummy = new ListNode(0);
        // Pointer to build the new list
        ListNode tempAns = dummy;

        // Traverse the original linked list
        while (head != null) {
            // If the current node's value is not in the set of values to remove
            if (!uniqueNodeValue.contains(head.val)) {
                // Create a new node with the current value and append it to the new list
                tempAns.next = new ListNode(head.val);
                // Move the pointer to the newly added node
                tempAns = tempAns.next;
            }
            // Move to the next node in the original list
            head = head.next;
        }

        // Return the head of the new list, skipping the dummy node
        return dummy.next;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.