0203. Remove Linked List Elements

203. Remove Linked List Elements #

Problem #

Remove all elements from a linked list of integers that have value val.

Example:


Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

Problem Summary #

Delete all nodes with the specified value from the linked list.

Solution Approach #

Just follow the problem statement.

Code #


package leetcode

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func removeElements(head *ListNode, val int) *ListNode {
	if head == nil {
		return head
	}
	newHead := &ListNode{Val: 0, Next: head}
	pre := newHead
	cur := head
	for cur != nil {
		if cur.Val == val {
			pre.Next = cur.Next
		} else {
			pre = cur
		}
		cur = cur.Next
	}
	return newHead.Next
}


Calendar Jun 25, 2026
Edit Edit this page
Total visits:   You are visitor No.
中文