avatar

superorange0707

  • Homepage
  • Tag
  • Category
Home Leetcode-203-Remove Linked List Elements
文章

Leetcode-203-Remove Linked List Elements

Posted 2022-07-15
8~10 min read

Remove Linked List Elements

leetcode: https://leetcode.com/problems/remove-linked-list-elements/

Description:

Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.

Idea:

**solution 1: move head node **

Step1: if the head node is target node, then delete it by removing head to it’s next unit the new head node isn’t the target

Step2: create temporary index to traverse the list when the current node isn’t null and the next node of current node isn’t null. if one element is the target, then make the current index point to the next next node.

​

Code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        //if the list is null
        if(head == null){
            return head;
        }
        //when the head is target, make the next node as head.
        while(head != null && head.val == val){
            head = head.next;
        }
        //set the index to traverse the list
        ListNode current = head;
        while(current != null){
            //when the next node isn't null, and this node is targe,make current node point to next next node
            while(current.next != null && current.next.val == val){
                current.next =current.next.next;
            }
            //if the current's next node isn't target, move it to the next
            current =current.next;
        }
        return head;
    }
}

Solution 2: Use Dummy Node

Step1: create a new node, and make the next node of it point to the original head node.

Step2: because now the head is new virtual node, so we don’t need to consider the head node again.

Step3; set the current node start from the head node, just search the list, if the element’s val == target, then make the current node point to next next node.

Code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        //if the list is null
        if(head == null){
            return head;
        }
        //set the dummy node and make it as new head by making it point to original node
        ListNode dummy = new ListNode(-1, head);
        //set the index to traverse the list
        ListNode current = dummy;
        while(current != null){
            //when the next node isn't null, and this node is targe,make current node point to next next node
            while(current.next != null && current.next.val == val){
                current.next =current.next.next;
            }
            //if the current's next node isn't target, move it to the next
            current =current.next;
        }
        //dummy's next node is real list
        return dummy.next;
    }
}
Leetcode
Leetcode Linked List
Share

Further Reading

Apr 23, 2025

283 - Move Zero

[283 - Move Zero] 🔗 LeetCode Link Problem Description Given an integer array nums, move all 0's to the end of it while maintaining the relative order

Apr 23, 2025

27 - Remove Element

[27 - Remove Element] 🔗 LeetCode Link Problem Description Given an integer array nums and an integer val, remove all occurrences of val in nums in-pl

Apr 23, 2025

26 - Remove Duplicates from Sorted Array

[26 - Remove Duplicates from Sorted Array] 🔗 LeetCode Link Problem Description Given an integer array nums sorted in non-decreasing order, remove the

OLDER

Leetcode-18-4Sum

NEWER

Leeetcode-707-Design Linked List

Recently Updated

  • Migrating Jenkins SCM Using GitLab from Bitbucket: SCM URL Bulk Replacement
  • 283 - Move Zero
  • 27 - Remove Element
  • 26 - Remove Duplicates from Sorted Array
  • Migrating from Bitbucket to GitLab? Here’s how to keep your teams moving without missing a beat!

Trending Tags

Course two pointer Binary Tree Hash SQL Leetcode Error Recording Gitlab Bitbucket Devops

Contents