Delete a Linked-List Node Given Only Its Pointer
Updated
Problem statement
A singly linked list contains a node you must delete, but all you get is node, a reference to that node. You do not get the head of the list or the node before node. Any nodes before node keep pointing to it.
Delete the element held by node: afterwards, reading the list from node onward must give the same values in the same order, with that one element removed. Then return node.
In each test, the input lists the values of node and of the nodes after it, starting with node. The nodes before it are not shown. The judge reads the list starting from the node you return. The judge compares only those values, so it cannot check that you changed the list in place: returning node.next would also pass, but it would not delete anything.
Function
deleteNode(node: ListNode) → ListNodeExamples
Example 1
node = [6,2,9,4]return = [2,9,4]The input is node, holding 6, followed by nodes holding 2, 9 and 4. Copy 2 from the next node into node, then link node past that next node. Any nodes before node still point to it, and the list from node now reads 2, 9, 4.
Example 2
node = [-3,-3,0]return = [-3,0]node and the node after it both hold -3. Deleting node removes only one of them, so one -3 is left, followed by 0.
Example 3
node = [10,7]return = [7]node is the second-to-last node. After 7 is copied into it and the last node is skipped, node becomes the last node and holds 7.
Constraints
- The list that starts at
nodehas between2and1000nodes, sonodeis never the last node. - Every node value is between
-10^5and10^5. - Values may repeat.
More Nutanix Onsite Interview questions
- Design Hit CounterONSITE INTERVIEW · Seen Oct 2026
- Peak Process MemoryONSITE INTERVIEW · Seen Aug 2026
- Minimum Operations to Transform ArrayONSITE INTERVIEW · Seen Jun 2026
- Count Connected Components in an Undirected GraphONSITE INTERVIEW · Seen Aug 2025
- LRU CacheONSITE INTERVIEW · Seen Aug 2025
- Running Median of a Multilevel Linked List in Level OrderONSITE INTERVIEW · Seen Aug 2025
- Custom Array IteratorONSITE INTERVIEW · Seen Dec 2024
- Collapse Runs of Repeated CharactersONSITE INTERVIEW · Seen Nov 2022
- Anagram Words in a SentenceONSITE INTERVIEW · Seen Apr 2022
- Sudoku SolverONSITE INTERVIEW · Seen Feb 2022
Similar questions at other companies
- Detect a Loop in a Linked ListEPAM · ONSITE INTERVIEWEPAM interview questions
- Delete a Doubly Linked List Node at a PositionQualys · ONSITE INTERVIEWQualys interview questions
- Insert After a Linked-List NodeAMD · ONSITE INTERVIEWAMD interview questions
- Reverse a Singly Linked ListGoogle · ONSITE INTERVIEWGoogle interview questions
- Process Singly Linked List OperationsSEDEMAC · ONSITE INTERVIEW
- Merge Two Sorted Linked ListsZS · ONSITE INTERVIEWZS interview questions
- Middle Node Of A Linked ListNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Build and Check a Palindrome Linked ListOmnissa · ONSITE INTERVIEWOmnissa interview questions
- Reverse a Singly Linked ListGEP · ONSITE INTERVIEW
- Merge Two Sorted Linked ListsApple · ONSITE INTERVIEWApple interview questions