Topic:
Please modify the Append function to use this function to implement:
The set of two non-descending linked lists, 1->2->3 and 2->3->5, and 1->2->3->5
In addition, you can only output the results, you can not modify the data of two linked lists.
Analysis:
This question is very simple, two pointers to the linked list, compare the corresponding values, and traverse
Implemented as follows:
#include <iostream> using namespace std;
struct node{Node (int _v = 0): Value (_v), Next (NULL) {} int value;
Node *next;
};
void Printnodes (node* N1, node* n2) {node* P1 = n1;
node* P2 = n2;
while (P1!= null && p2!= null) {if (P1->value < P2->value)
{cout << p1->value << "->";
P1 = p1->next; else if (P1->value > P2->value) {cout << p2
->value << "->";
P2 = p2->next; else {if (P1->next = null && P2->next = NULL
) cout << p2->value << Endl;
Else cout << p2->value << "->";
P2 = p2->next;
P1 = p1->next;
}} while (P1!= null) {if (P1->next = null)
cout << p1->value << Endl;
else cout << p1->value << "->";
P1 = p1->next; } while (P2!= null) {if (P2->next = null) cout << ;
P2->value << Endl;
else cout << p2->value << "->";
P2 = p2->next;
int main () {Node N1 (1), N2 (2), N3 (3), N4 (2), N5 (3), N6 (5);
node* Node1 = &n1;
Node1->next = &n2;
Node1->next->next = &n3;
node* Node2 = &n4; node2-≫next = &n5;
Node2->next->next = &n6;
cout << "List1:";
Printnodes (Node1, NULL);
cout << "List2:";
Printnodes (Node2, NULL);
cout << "List1 and List2:";
Printnodes (Node1, Node2);
return 0; }
The output is as follows:
List1:1->2->3
List2:2->3->5
List1 and List2:1->2->3->5
Author: csdn Blog hhh3h
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/