Create a function expand, which takes in a pointer to the head of the linked lis
ID: 3826740 • Letter: C
Question
Create a function expand, which takes in a pointer to the head of the linked list, and returns a pointer to the head of a linked list in which all strings repeat following the rules described above.
Example:
"apple"->NULL
return "apple"->"apple"->NULL
"green"->"green"->"blue"->"red"->"green"->NULL
return "green"->"green"->"blue"->"blue"->"red"->"red"->"green"->"green"->NULL
The linked lists for this problem use the following class declaration:
class node {
public: string data;
node* next;
};
Do this in C++. Code in bold CANNOT be changed:
node* expand(node* head) {
while(head != NULL) {
head->data = head->data+head->next;
head = head->next;
}
}
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
class node {
public:
string data;
node* next;
};
Do this in C++. Code in bold CANNOT be changed:
node* expand(node* head) {
node *temp = head;
while(temp != NULL){
node *newNode = new node;
node->data = temp->data;
newNode->next = temp->next;
temp->next = newNode;
temp = newNode->next;
}
return head;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.