简体   繁体   中英

Can someone explain the algorithm?

I need function with parameters as (LinkedList one,LinkedList two)

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???

to check if a list is subset of another, I have this code from 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. Well, I see that your code is from geeksforgeeks . The geeksforgeeks assumes that you have your own linked list implementation. It also assumes you have access to the next and data parts of the linked list nodes. Unfortunately, java LinkedList does not expose those, so your code is not useful for your question.

You need to design a new algorithm for the Java LinkedList . Since LinkedList is not a Set . It is not very meaningful to execute set functions over a 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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