如何在这两个分类的链接列表之间获得开关的位置

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

本文关键字:列表 链接 之间 位置 开关 分类 两个      更新时间:2023-10-16

我一直在为这项作业挣扎了很长时间,以至于我非常感谢我能得到的任何帮助。

我遇到的问题很简单,但有点令人困惑。

因此,解决了问题的很大一部分(如第一个代码注释中所述 - 找到这两个排序的链接列表之间的最大总路径(

/*
|--------------------------------------------------------------------------
| 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 Listn";
        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;
}

我无法动手的是两个链接列表之间的开关,在示例中,它们应该为3&amp;240

一个开关是该路径继续以获取最大总和路径的数字,并且它只能是两个列表之间的常见节点。

geeksforgeeks上的原始问题

您可以创建一个滑动窗口,滑动窗口从第一个相交点到第二个。对于两个链接列表,存在两个滑动窗口。两者中具有较大总和的滑动窗口必须在新的链接列表中。在滑动窗口的第一个交叉点之后,您可以有两个临时变量指向每个链接列表中的第一个元素。一旦找出路径,您就可以将交叉节点指向该地址。