Microsoft face question resolution: Please modify the append function, using function implementation (linked list)

Source: Internet
Author: User

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 <&lt ;  
                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-&Gt;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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.