I am to take two nodes, defined by:
And create a pointer node to a list of those two nodes sorted alphabetically. It's a simple assignment, but apparently something's going awry. The incoming values of the node's next is irrelevant, but the output matters and the last node in the list must point to null.Code:struct Node{ string val; Node *next; };
I am failing a test of some sort (and the only clue as to what it tests is 'pointers'), but I can't tell what I did wrong.Code:Node* sortPair (Node* p1, Node* p2) { assert(p1!=NULL); assert(p2!=NULL); Node *p3 = new Node; if (p1->val <= p2->val) { p3->next = p1; p1->next = p2; p2->next = NULL; } else { p3->next = p2; p2->next = p1; p1->next = NULL; } return p3; }
Help is very much appreciated.