Loading Question...
You are given the head of a linked list containing integer values. Create a new linked list by:
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;
}
Microsoft • Pending
Google • Pending
Google • Pending
Google • Pending