This is a verified interview question from Microsoft. Candidates reporting seeing this problem in recent Online Assessments (OAs) and onsite rounds. Mastering "Delete Odd Nodes from a Linked List - Microsoft Online Assessment IGDTUW" covers key patterns like Arrays.
"You are given the head of a linked list containing integer values. Create a new linked list by: * Removing all nodes with odd values * Keeping only nodes with even values * Preserving the original order of the remaining nodes Return a reference to the head of the resulting linked list. For example, your linked list values are 2 → 1 → 3 → 4 → 6. Your new linked list should be 2 → 4 → 6. Constraints * * Test Case Input Format The first line contains an integer n, the number of nodes in the list. Each of the next n lines contains an integer node->data. ```text Starter Code (C++14) #include <bits/stdc++.h> using namespace std; /* * SinglyLinkedListNode struct for reference */ class SinglyLinkedListNode { public: int data; SinglyLinkedListNode *next; SinglyLinkedListNode(int node_data) { this->data = node_data; this->next = nullptr; } }; class SinglyLinkedList { public: SinglyLinkedListNode *head; SinglyLinkedListNode *tail; SinglyLinkedList() { this->head = nullptr; this->tail = nullptr; } void insert_node(int node_data) { SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data); if (!this->head) { this->head = node; } else { this->tail->next = node; } this->tail = node; } }; void print_singly_linked_list(SinglyLinkedListNode* node, string sep) { while (node) { cout << node->data; node = node->next; if (node) { cout << sep; } } } /* * Complete the 'deleteOdd' function below. * * The function is expected to return an INTEGER_SINGLY_LINKED_LIST_NODE. * The function accepts INTEGER_SINGLY_LINKED_LIST_NODE listHead as parameter. */ SinglyLinkedListNode* deleteOdd(SinglyLinkedListNode* listHead) { // Write your code here } int main() { SinglyLinkedList* list = new SinglyLinkedList(); int list_count; if (!(cin >> list_count)) return 0; for (int i = 0; i < list_count; i++) { int list_item; cin >> list_item; list->insert_node(list_item); } SinglyLinkedListNode* result = deleteOdd(list->head); print_singly_linked_list(result, "\n"); cout << "\n"; return 0; } ```"
Join thousands of developers practicing for Microsoft.