In: Computer Science
Write the code that will produce the given "after" result from the given "before" starting point by modifying links between the nodes shown. Assume that the nodes have already been declared and initialized to match the "before" figure below. There may be more than one way to write the code, but do NOT change any existing node's data field value. Do not create any new nodes, you must just rearrange the existing nodes.
If a variable does not appear in the "after" picture, it doesn't matter what value it has after the changes are made. If a given node object does not appear in the "After" picture, you must free its memory to avoid a memory leak.
You should not be depending on the values of the nodes, the second test case here will test a list1->a->b->c->d (some other list of the values). And it must be re-arranged in the same way.
Before |
list1: 1 -> 2 -> 3 -> 4 / |
---|---|
After |
list1: 3 -> 2 / list2: 4 -> 1 / |
Assume that you are using the following ListNode structure:
struct ListNode {
int data; // data stored in this node
ListNode* next; // a link to the next node in the list
};
Edit here:
#pragma once
#include <iostream>
using namespace std;
struct ListNode {
int data;
ListNode* next;
};
void pointerFun(ListNode* &list1, ListNode* &list2)
{
// TODO: write your code here
}
#pragma once
#include <iostream>
using namespace std;
struct ListNode {
int data;
ListNode* next;
};
void pointerFun(ListNode* &list1, ListNode* &list2)
{
ListNode* temp = NULL;
// loop to delete existing the nodes of list2 (if
any)
while(list2 != NULL)
{
temp = list2;
list2 = list2->next;
temp->next = NULL;
delete temp;
temp = NULL;
}
int count = 0;
ListNode* curr = list1;
// loop to keep first 4 nodes of list1 in list1 and
keep the rest nodes to list2
while(curr != NULL)
{
count++;
if(count == 4)
{
list2 =
curr->next;
curr->next =
NULL;
}
curr = curr->next;
}
// loop to delete all the nodes after the first 4
nodes of list1 (if any)
while(list2 != NULL)
{
temp = list2;
list2 = list2->next;
temp->next = NULL;
delete temp;
temp = NULL;
}
// list1 -> 1 -> 2 -> 3 -> 4 ->
NULL
temp = NULL;
curr = list1;
// loop to get the last and second last node
while(curr->next != NULL)
{
temp = curr;
curr = curr->next;
}
// remove the last node from list1
temp->next = NULL; // temp : 3 -> NULL
// set list2 to last node
list2 = curr; // list2 : 4 -> NULL
// set temp to first node
temp = list1; // temp : 1 -> 2 -> 3 ->
NULL
// set list1 to second node
list1 = list1->next; // list1 : 2 -> 3 ->
NULL
// add temp to list2
// set next of temp to null
temp->next = NULL; // temp : 1 -> NULL
// set next of list2 to temp
list2->next = temp; // list2 : 4 -> 1 ->
NULL
// set temp to previous third node
temp = list1->next; // temp : 3 -> NULL
// set next of list1 to null
list1->next = NULL; // list1: 2 -> NULL
// set next of temp to list1
temp->next = list1; // temp: 3 -> 2 ->
NULL
// update list1 to temp
list1 = temp; // list1: 3 -> 2 -> NULL
}
// testing the function
int main()
{
ListNode *list1, *list2 = NULL, *curr;
ListNode *node;
list1 = new ListNode;
list1->data = 1;
list1->next = NULL;
node = new ListNode;
node->data = 2;
node->next = NULL;
list1->next = node;
curr = list1->next;
node = new ListNode;
node->data = 3;
node->next = NULL;
curr->next = node;
curr = curr->next;
node = new ListNode;
node->data = 4;
node->next = NULL;
curr->next = node;
curr = curr->next;
node = new ListNode;
node->data = 5;
node->next = NULL;
curr->next = node;
curr = curr->next;
node = new ListNode;
node->data = 6;
node->next = NULL;
curr->next = node;
curr = curr->next;
node = new ListNode;
node->data = 7;
node->next = NULL;
curr->next = node;
curr = curr->next;
node = new ListNode;
node->data = 10;
node->next = NULL;
list2 = node;
curr = list2;
node = new ListNode;
node->data = 12;
node->next = NULL;
curr->next = node;
curr = curr->next;
node = new ListNode;
node->data = 15;
node->next = NULL;
curr->next = node;
curr = curr->next;
node = new ListNode;
node->data = 18;
node->next = NULL;
curr->next = node;
curr = curr->next;
cout<<"Before: "<<endl<<"List1: ";
curr = list1;
while(curr != NULL)
{
cout<<curr->data<<" -> ";
curr = curr->next;
}
cout<<"NULL"<<endl;
cout<<"List2: ";
curr = list2;
while(curr != NULL)
{
cout<<curr->data<<" -> ";
curr = curr->next;
}
cout<<"NULL"<<endl;
pointerFun(list1, list2);
cout<<"After: "<<endl<<"List1: ";
curr = list1;
while(curr != NULL)
{
cout<<curr->data<<" -> ";
curr = curr->next;
}
cout<<"NULL"<<endl;
cout<<"List2: ";
curr = list2;
while(curr != NULL)
{
cout<<curr->data<<" -> ";
curr = curr->next;
}
cout<<"NULL";
return 0;
} //end of testing the function
Output: