简体   繁体   English

如何获得这2个排序的链表之间的开关的位置

[英]How can I get the location of switches between these 2 sorted linked lists

I've been struggling with this homework for so long that I really appreciate any help I can get. 我一直在为此作业苦苦挣扎,以至于我真的很感激我能得到的任何帮助。

The problem I am stuck on is pretty simple but a bit confusing. 我遇到的问题很简单,但有点令人困惑。

So the big part of the problem is solved (As mentioned in the first code comment — to find the maximum sum path between these two sorted linked lists) 这样就解决了问题的绝大部分(如第一个代码注释中提到的那样-找到这两个排序的链表之间的最大和路径)

/*
|--------------------------------------------------------------------------
| ADSL Assignment 1 | Maximum Sum Problem.                                |
|--------------------------------------------------------------------------
| * You are provided with two sorted lists as input.
| * Those lists have some nodes in common.
| * Find a new list composed of the union of the nodes of those two lists.
| * To create the new list, you should take parts (paths of one or more nodes) from each list and merge them together.
| * You are allowed to switch between the two original lists only at intersections (same node in both lists).
| * in order to figure out the resulting list having the path that represents the greatest maximum total.
| Input:
|   * Two Sorted Linked Lists.
| Output:
|   * New list containing maximum sum path.
|   * Location of switches between the 2 lists.
*/
#include <iostream>
using namespace std;
class Node {
public:
    int num;
    Node* next;
    Node(int data, Node* next = 0) {    this->num = data;  this->next = next;     }
    Node() {    this->next = 0;     }
};
class List {
public:
    Node* head;
    Node* tail;
    List() {    head = tail = 0;    }

    void AddToTail(int data) {
        if (head == 0) head = tail = new Node(data);
        else {
            tail->next = new Node(data);
            tail = tail->next;
            tail->next = 0;
        }
    }

    void Print() {
        Node* temp = head;
        if (temp == 0) cout << "Empty List\n";
        while(temp != 0) {
            cout << temp->num << "\t";
            temp = temp->next;
        }
        cout << "\n";
    }

};

List MaxSumPath(Node* head1, Node* head2)
{
    List    l;

    Node*   temp1   = 0;
    Node*   temp2   = 0;
    Node*   temp    = 0;
    Node*   result  = 0;
    int     sum1    = 0;
    int     sum2    = 0;

    while (head1 || head2) {

        temp1 = head1;
        temp2 = head2;
        sum1 = sum2 = 0;

        while (head1 && head2)
        {
            if (head1->num < head2->num)
            {
                sum1 += head1->num;
                head1 = head1->next;
            }
            else if (head2->num < head1->num)
            {
                sum2 += head2->num;
                head2 = head2->next;
            }
            else break;
        }


        if (head1 == 0)
        {
            while (head2){
                sum2 += head2->num;
                head2 = head2->next;
            }
        }

        if (head2 == 0)
        {
            while (head1){
                sum1 += head1->num;
                head1 = head1->next;
            }
        }

        if (sum1 >= sum2)
        {

            if (result == 0) {
                result = temp1;
                temp = head1;
            }
            else {
                temp->next = temp1;
                temp = head1;
            }
        }

        else if (sum1 < sum2) {

            if(result == 0) {
                result = temp2;
                temp = head2;
            }
            else {
                temp->next = temp2;
                temp = head2;
            }
        }

        if (head1 && head2 && temp) {
            head1 = head1->next;
            head2 = head2->next;
            temp->next = 0;
        }
    }

    while (result)
    {
        l.AddToTail(result->num);
        result = result->next;
    }
    return l;
}


int main() {

    List l1;
    l1.AddToTail(1);
    l1.AddToTail(3);
    l1.AddToTail(30);
    l1.AddToTail(90);
    l1.AddToTail(120);
    l1.AddToTail(240);
    l1.AddToTail(511);

    List l2;
    l2.AddToTail(0);
    l2.AddToTail(3);
    l2.AddToTail(12);
    l2.AddToTail(32);
    l2.AddToTail(90);
    l2.AddToTail(125);
    l2.AddToTail(240);
    l2.AddToTail(249);


    cout << "List 1 = ";
    l1.Print();
    cout << "List 2 = ";
    l2.Print();

    cout << "---------------------------" << endl;

    cout << "List 3 = ";

    List result = MaxSumPath(l1.head, l2.head);
    result.Print();

    return 0;
}



What I can't get my hands on is the switches between two these linked lists, in the example given they should be 3 & 240 我无法动手的是在这两个链接列表之间进行切换,在给出的示例中,它们应该分别为3和240

A switch is a number that the path continue from in order to get to the maximum sum path, and it only can be a common node between the two lists. 开关是路径从其延续到最大和路径的数字,它只能是两个列表之间的公共节点。

The original problem on geeksforgeeks geeksforgeeks的原始问题

You can create a sliding window where the sliding window runs from point of first intersection to second. 您可以创建一个滑动窗口,其中滑动窗口从第一个交点到第二个交点。 For two linked lists, there exist two sliding windows. 对于两个链接列表,存在两个滑动窗口。 The sliding window with larger sum of the two has to be in the new linked list. 两者之和较大的滑动窗口必须位于新的链接列表中。 You can have two temp variables pointing to the first element in each linked list after the sliding window's first intersection. 在滑动窗口的第一个交点之后,可以有两个临时变量指向每个链接列表中的第一个元素。 And you can point the intersection node to that address once you figure out the path. 一旦确定了路径,就可以将交叉点指向该地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM