📌  相关文章
📜  从三个链表中求和等于给定数的三元组的程序

📅  最后修改于: 2022-05-13 01:55:28.167000             🧑  作者: Mango

从三个链表中求和等于给定数的三元组的程序

给定三个链表,例如 a、b 和 c,从每个链表中找到一个节点,使得节点的值之和等于给定数。
例如,如果三个链表分别为 12->6->29、23->5->8 和 90->20->59,给定数为 101,则输出应为三倍“6 5 90 ”。
在以下解决方案中,为了分析的简单性,假设所有三个链表的大小相同。以下解决方案也适用于不同大小的链表。

解决这个问题的一个简单方法是运行三个嵌套循环。最外面的循环从列表 a 中挑选一个元素,中间的循环从 b 中挑选一个元素,最里面的循环从 c 中挑选。最里面的循环还检查 a、b 和 c 的当前节点的值的总和是否等于给定的数字。该方法的时间复杂度为 O(n^3)。
排序可用于将时间复杂度降低到 O(n*n)。以下是详细步骤。
1)按升序对列表b进行排序,按降序对列表c进行排序。
2) b 和 c 排序后,从列表 a 中一个一个地挑选一个元素,通过遍历 b 和 c 找到对。请参阅以下代码中的 isSumSorted()。这个想法类似于 3 和问题的二次算法。

以下代码仅实现第 2 步。通过添加此处讨论的合并排序代码,可以轻松修改未排序列表的解决方案。

C#
// C# program to find a triplet 
// from three linked lists with
// sum equal to a given number
using System;
  
public class LinkedList
{
    public Node head; // head of list
  
    /* Linked list Node*/
    public class Node
    {
        public int data;
        public Node next;
        public Node(int d) 
        {
            data = d; next = null;
        }
    }
  
    /* A function to check if there 
    are three elements in a, b
    and c whose sum is equal to 
    givenNumber. The function
    assumes that the list b is 
    sorted in ascending order and
    c is sorted in descending order. */
bool isSumSorted(LinkedList la, LinkedList lb, 
                LinkedList lc, int givenNumber)
{
    Node a = la.head;
  
    // Traverse all nodes of la
    while (a != null)
    {
        Node b = lb.head;
        Node c = lc.head;
  
        // for every node in la pick 
        // 2 nodes from lb and lc
        while (b != null && c!=null)
        {
            int sum = a.data + b.data + c.data;
            if (sum == givenNumber)
            {
                Console.WriteLine("Triplet found " + a.data +
                                    " " + b.data + " " + c.data);
                return true;
            }
  
            // If sum is smaller then 
            // look for greater value of b
            else if (sum < givenNumber)
                b = b.next;
  
            else
                c = c.next;
        }
        a = a.next;
    }
    Console.WriteLine("No Triplet found");
    return false;
}
  
  
    /* Given a reference (pointer to pointer) to the head
    of a list and an int, push a new node on the front
    of the list. */
    void push(int new_data)
    {
        /* 1 & 2: Allocate the Node &
                Put in the data*/
        Node new_node = new Node(new_data);
  
        /* 3. Make next of new Node as head */
        new_node.next = head;
  
        /* 4. Move the head to point to new Node */
        head = new_node;
    }
  
    /* Driver code*/
    public static void Main(String []args)
    {
        LinkedList llist1 = new LinkedList();
        LinkedList llist2 = new LinkedList();
        LinkedList llist3 = new LinkedList();
  
        /* Create Linked List llist1 100->15->5->20 */
        llist1.push(20);
        llist1.push(5);
        llist1.push(15);
        llist1.push(100);
  
        /*create a sorted linked list 'b' 2->4->9->10 */
        llist2.push(10);
        llist2.push(9);
        llist2.push(4);
        llist2.push(2);
  
        /*create another sorted linked list 'c' 8->4->2->1 */
        llist3.push(1);
        llist3.push(2);
        llist3.push(4);
        llist3.push(8);
  
        int givenNumber = 25;
        llist1.isSumSorted(llist1,llist2,llist3,givenNumber);
    }
}
  
// This code contributed by Rajput-Ji


输出:

Triplet Found: 15 2 8

时间复杂度:链表 b 和 c 可以使用合并排序在 O(nLogn) 时间内排序(参见此)。步骤 2 需要 O(n*n) 时间。所以整体时间复杂度是 O(nlogn) + O(nlogn) + O(n*n) = O(n*n)。
在这种方法中,链表 b 和 c 首先排序,因此它们的原始顺序将丢失。如果我们想保留 b 和 c 的原始顺序,我们可以创建 b 和 c 的副本。

有关详细信息,请参阅有关从三个链表中查找总和等于给定数字的三元组的完整文章!