FastPrepRemove Linked List Elements
Problems/Remove Linked List Elements

Remove Linked List Elements

Microsoft logoMicrosoft● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A review of a Microsoft software engineering internship lists a linked-list deletion task from the coding interview, with an extra ask to show more than one way to solve it.

The report does not say which node goes, how the list is given, or any limits. Here you remove every node that holds a given value and return the new head, and an empty result is allowed. The more-than-one-way part is a follow-up in the statement and editorial and is not tested.

Our version is about 60% the same as the reported question.

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) → ListNode

Examples

Example 1

head = [7,3,7,7,5,7]val = 7return = [3,5]
head
737757
Removed
→
return
35
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 = []
head
empty
return
empty

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 = []
head
222
return
empty

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 SWE Intern Interviews: 244 Experiences (Part 1 of 6)

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)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
/**
 * 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) {
        // Write your code here.
    }
}
Checking account…