logo CodeStepByStep logo

123-45-251-34

Language/Type: C++ linked lists pointers
Author: Marty Stepp (on 2016/08/27)

Write the code that will produce the given "after" result from the given "before" starting point by modifying links between the nodes shown and/or creating new nodes as needed. 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.

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.

Before
list1: 1 -> 2 -> 3 /
list2: 4 -> 5 /
After
list1: 2 -> 5 -> 1 /
list2: 3 -> 4 /

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

    ListNode(int data = 0, ListNode* next = NULL) { ... }   // constructor
};
Type your C++ solution code here:


This problem asks for bare code. Submit a fragment of C++ code as described. Do not write any main function or any function heading around your code; just write the lines of code that will produce the result described.

You must log in before you can solve this problem.


Log In

Need help?

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.