Remove Cancelled Orders From a Singly Linked Order Chain

Last updated: November 29, 2026

Quick Overview

Given the head of a singly linked list of order records and a set of cancelled order IDs, remove every node whose ID is cancelled and return the new head. Do it in one pass with O(1) extra space beyond the input set.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Given the head of a singly linked list of order records and a set of cancelled order IDs, remove every node whose ID is cancelled and return the new head. Do it in one pass with O(1) extra space beyond the input set.

A warm-up style question common in Amazon online assessments for new-grad and SDE I candidates. The interviewer checks pointer hygiene, especially when the head itself or consecutive nodes must be removed.

What the Interviewer Expects
  • Use a dummy (sentinel) node so head removal needs no special case
  • Advance the previous pointer only when the next node is kept
  • Handle runs of consecutive cancelled nodes correctly
  • State O(n) time and O(1) extra space
  • Test with an all-cancelled list and an empty list
Key Topics to Cover
linked lists
sentinel node
in-place deletion
hash set lookup
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you do this recursively, and what is the stack risk for a million-node chain?
  • What changes if the list is doubly linked?
  • How would you remove cancelled orders when the cancellation set is too large for memory?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input is the head of a singly linked list of order IDs and a set cancelled. Output is the head of the list with every cancelled node unlinked, order of survivors unchanged. The list may be empty, th...

High-Level Design

Put a dummy node in front of the head. Walk with a pointer prev that always points at the last node we decided to keep. Look at prev.next: if it is cancelled, splice it out by setting `prev.next =...


Submit Your Answer
Markdown supported