简体   繁体   English

有人可以解释一下算法吗?

[英]Can someone explain the algorithm?

I need function with parameters as (LinkedList one,LinkedList two)我需要 function 参数为(LinkedList 一,LinkedList 二)

so, how to set/define the head and current for both the list seperately??那么,如何分别设置/定义列表的头部和电流?

I dont know why this question was closed.我不知道为什么这个问题被关闭了。 But I am new to java and need to solve this so can anybody help???但我是 java 的新手,需要解决这个问题,所以有人可以帮忙吗???

to check if a list is subset of another, I have this code from GeeksforGeeks要检查一个列表是否是另一个列表的子集,我有来自GeeksforGeeks的这段代码

HERE IT IS CODE IF WE PASS NODE IN THE PARAMETER LIKE (Node one,Node two) but I want parameters as (linkedlist one,liked list two) so can anyone explain algorithm to do so???这是代码如果我们在参数中传递节点喜欢(节点一,节点二)但我想要参数为(链表一,喜欢的列表二)所以任何人都可以解释算法这样做吗?

static boolean checkSubSet(Node first, Node second) { 
    Node ptr1 = first, ptr2 = second; 
  
    // If both linked lists are empty, 
    // return true 
    if (first == null && second == null) 
        return true; 
  
    // Else If one is empty and  
    // other is not, return false 
    if (first == null || 
       (first != null && second == null)) 
        return false; 
  
    // Traverse the second list by  
    // picking nodes one by one 
    while (second != null) 
    { 
        // Initialize ptr2 with  
        // current node of second 
        ptr2 = second; 
  
        // Start matching first list  
        // with second list 
        while (ptr1 != null) 
        { 
            // If second list becomes empty and  
            // first not then return false 
            if (ptr2 == null) 
                return false; 
  
            // If data part is same, go to next 
            // of both lists 
            else if (ptr1.data == ptr2.data) 
            { 
                ptr1 = ptr1.next; 
                ptr2 = ptr2.next; 
            } 
  
            // If not equal then break the loop 
            else break; 
        } 
  
        // Return true if first list gets traversed 
        // completely that means it is matched. 
        if (ptr1 == null) 
            return true; 
  
        // Initialize ptr1 with first again 
        ptr1 = first; 
  
        // And go to next node of second list 
        second = second.next; 
    } 
    return false; 
} 

but how to do the same thing by passing the actual linked lists as a parameter for eg但是如何通过将实际的链表作为参数传递来做同样的事情,例如

static boolean checkSubSet(Node first, Node second){}

instead of this I want to do this而不是这个我想做这个

static boolean checkSubSet(LinkedList<Integer> list1,LinkedList<Integer> list2){} 

You are trying to refactor your code so it accepts java.util.LinkedList as an argument.您正在尝试重构代码,使其接受java.util.LinkedList作为参数。 Well, I see that your code is from geeksforgeeks .好吧,我看到您的代码来自geeksforgeeks The geeksforgeeks assumes that you have your own linked list implementation. geeksforgeeks假设您有自己的链表实现。 It also assumes you have access to the next and data parts of the linked list nodes.它还假设您可以访问链表节点的nextdata部分。 Unfortunately, java LinkedList does not expose those, so your code is not useful for your question.不幸的是, java LinkedList没有公开这些,因此您的代码对您的问题没有用处。

You need to design a new algorithm for the Java LinkedList .您需要为 Java LinkedList设计一个新算法。 Since LinkedList is not a Set .由于LinkedList不是Set It is not very meaningful to execute set functions over a LinkedList .LinkedList上执行集合函数并不是很有意义。 However, if you really need, you may use something like that:但是,如果你真的需要,你可以使用类似的东西:

return new HashSet(a).containsAll(new HashSet(b));

or, iterate over the lists to get what you want.或者,遍历列表以获得您想要的。

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

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