Remove Linked List Elements
Updated
Problem statement
You are given the head of a singly linked list and an integer val. Remove every node whose value equals val, and return the head of the list that is left.
The nodes that stay keep their original order. If every node is removed, return an empty list.
Follow-up: solve it in more than one way, for example by building a new list, with recursion, and by unlinking nodes in place. What does each way cost in time and extra memory?
Function
removeElements(head: ListNode, val: int) → ListNodeExamples
Example 1
head = [7,3,7,7,5,7]val = 7return = [3,5]Removed
Moved or new
The nodes with value 7 are the first node, the third and fourth nodes, and the last node. Removing them leaves 3 and then 5, in their original order.
Example 2
head = []val = 4return = []The list is empty, so there is nothing to remove and the answer is still an empty list.
Example 3
head = [2,2,2]val = 2return = []Every node has value 2, so every node is removed and the answer is an empty list.
Constraints
- The number of nodes in the list is in the range
[0, 5000]. -1000 <= Node.val <= 1000-1000 <= val <= 1000
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Reverse a Singly Linked ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Remove Linked List Nodes with a Given ValueExpedia · ONSITE INTERVIEWExpedia interview questions
- Sorted Doubly Linked List to Balanced BST In PlaceSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Reverse Nodes in K-GroupAmazon · ONSITE INTERVIEW
- Merge K Sorted Linked ListsThe Walt Disney Company · OA · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Merge K Sorted ListsSkydio · ONSITE INTERVIEWSkydio interview questions
- Merge K Sorted Linked Lists Into OneCapital One · ONSITE INTERVIEWCapital One interview questions
- Reverse a Linked ListCisco · PHONE SCREENCisco interview questions
- Reverse a Linked ListApple · PHONE SCREENApple interview questions
- Reverse a Singly Linked ListChubb · PHONE SCREENChubb interview questions
Interview reports that mention this question
Microsoft interview experiences
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
- Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)