Amazon Coding Question – Solved

8 Live
Delete Even Given a linked list of integers, return a linked list containing only nodes having odd integers in their node->data field. Example n=3 listHead=1->4->7 The linked list containing only nodes having odd integers is 1 -> 7. Function Description Complete the function deleteEven in the editor below. deleteEven has the following parameter(s): list listHead: a reference to the

Asked in: Amazon

Image of the Question

Question Image

All Testcases Passed ✔



Passcode Image

Solution


import math
import os
import random
import re
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve the problem of removing all nodes with even integers from a linked list and returning a list containing only nodes with odd integers, you should start by understanding the nature of linked lists and how traversal and modification work within them.

First, the linked list is a data structure where each node contains some data (here, integers) and a reference (or pointer) to the next node. Your task involves filtering out nodes based on a condition—whether their data is odd or even.

Step 1: Understand the Problem Requirements
- You have a singly linked list where each node contains an integer.
- You need to construct a new list (or modify the existing one) such that it contains only nodes with odd values.
- Nodes with even integers should be removed from the list.
- The order of nodes with odd values should remain the same as in the original list.

Step 2: Traversing the Linked List
- The natural way to examine each node is to traverse the linked list starting from the head.
- You’ll move node by node using the next pointer until you reach the end (where the next pointer is null).
- For each node, you check whether the value stored is odd or even.

Step 3: Modifying the List While Traversing
- Since you need to remove nodes with even values, you must consider how to update the links to "skip" these nodes.
- Removing a node in a singly linked list requires adjusting the next pointer of the preceding node to point to the node after the one being removed.
- Therefore, it’s important to keep track of the previous node as you traverse so you can link it properly when a node is removed.
- Special attention is needed for the head of the list because if the head node is even, you need to move the head pointer forward until you find the first odd node or reach the end.

Step 4: Handling Edge Cases
- If the list is empty initially, you can directly return an empty list or null.
- If all nodes are even, the final list will be empty.
- If the head node(s) are even, you must update the head to the first odd node to avoid dangling pointers.
- The list could have only one node which is either odd or even.

Step 5: Approach to Implementation
- Start by skipping any even nodes at the beginning until you reach the first odd node or the list ends.
- Then iterate through the rest of the list, maintaining two pointers: one to the current node and one to the previous node.
- When you encounter a node with an even value, update the previous node’s next pointer to skip this node and move forward.
- When you encounter a node with an odd value, move both pointers forward.
- Continue this process until you reach the end of the list.

Step 6: Efficiency Considerations
- This approach requires a single pass through the linked list, leading to a time complexity of O(n), where n is the number of nodes.
- The space complexity is O(1) as modifications are done in place, without creating new nodes or auxiliary data structures.

Step 7: Alternative Solutions and Their Trade-offs
- Another approach could involve creating a new linked list and appending only odd nodes from the original list to this new list.
- This would involve additional space but could simplify the logic by not needing to manage previous pointers.
- However, modifying the original list in place is usually more space-efficient and often preferred unless otherwise specified.

Step 8: Final Thoughts and Good Practices
- Remember to carefully handle pointer updates to avoid losing references or creating cycles.
- Testing with various inputs—empty list, all even, all odd, alternating even and odd—is essential to validate correctness.
- Clean code and clear logic for head updates and node removals make the solution maintainable and understandable.

Summary:
- Traverse the linked list once.
- Skip initial even nodes to find the new head.
- Maintain previous and current pointers.
- Remove even nodes by updating links.
- Return the head of the modified list containing only odd nodes.

This approach efficiently and correctly filters the linked list as required.
```


Related Questions