Direct operation and Set collection operation of de-duplicating and merging two ordered linked lists

Comparison of the two ideas:

Direct operation: Because two ordered linked lists are passed in, I will directly use one of the linked lists as the benchmark, compare it with the other linked list, and only insert the value larger than the last record of the returned value linked list, not Inserting equivalent values, the theoretical time complexity is O(n)

Set operation: Take out all nodes and put them into the TreeSet ordered set, and finally generate a linked list to return, the theoretical time complexity is O(2n)

Schematic diagram of direct operation steps:

Take {1, 3, 5}{1, 2, 4, 5, 5, 6} as an example

  1. First take the header of the return value linked list, and use the linked list as the benchmark linked list. Compare the first value with the smallest value as the benchmark linked list. If the same, take the first
    return value linked list: 1->null
    benchmark linked list: 3->5- >null
    non-reference linked list: 1->2->4->5->5->6->null
  2. Continue to the linked list after the next step. In this step, take the small value in the two linked lists and try to insert, but if the value at the end of the return value linked list is found to be the same, the
    return value linked list is discarded: 1->null
    benchmark linked list: 3->5-> Null
    non-reference linked list: 2->4->5->5->6->null
  3. Continue to the linked list after the next step. In this step, take the small value in the two linked lists and try to insert, but if the value at the end of the return value linked list is found to be different, insert the
    return value linked list: 1->2->null
    Reference linked list: 3-> 5->null
    non-reference linked list: 4->5->5->6->null
  4. Continue to the linked list after the next step. In this step, take the small value in the two linked lists and try to insert, but if the value at the end of the return value linked list is found to be different, insert the
    return value linked list: 1->2->3->null
    benchmark linked list: 5->null
    non-reference linked list: 4->5->5->6->null
  5. Continue to the linked list after the next step. In this step, take the small value in the two linked lists and try to insert, but if the value is found to be different from the end value of the return value linked list, insert the
    return value linked list: 1->2->3->4->null
    Reference list: 5->null
    Non-reference list: 5->5->6->null
  6. Continue to the linked list after the next step. In this step, take the small value in the two linked lists and try to insert, but if the value at the end of the return value linked list is found to be different, insert the
    return value linked list: 1->2->3->4->5 ->null
    benchmark linked list: null
    non benchmark linked list: 5->5->6->null
  7. After one of the linked lists is null, only the other chain is traversed directly. Because it is to be repeated, so after traversing, take a value in the non-reference linked list and try to insert, but if the value at the end of the return value linked list is found to be the same, the
    return value linked list is discarded: 1 ->2->3->4->5->null
    Reference list: null
    Non-reference list: 5->6->null
  8. Take a value in the non-reference linked list and try to insert, but if it finds that it is the same as the end value of the returned value linked list, discard the
    returned value linked list: 1->2->3->4->5->null
    benchmark linked list: null
    non benchmark linked list: 6->null
  9. Take a value in the non-reference linked list and try to insert it, but if it is found that it is different from the end value of the return value linked list, it will be retained. At this time, if both linked lists are null, the
    return value linked list will be ended: 1->2->3->4->5 ->6null
    benchmark linked list: null
    non benchmark linked list: null

The algorithm for the Set operation is not expanded

The comparison algorithm written by myself:

import java.util. * ;

public  class Main {

    public static void main(String[] args) {
        Node headOne = new Node(-1);
        Node headTwo = new Node(-1);
        Node p1 = headOne;
        Node p2 = headTwo;
        int[] one = new int[10000];
        int[] two = new int[15000];
        Random rm = new Random();
        for (int i = 0; i < 10000; i++){
            one[i] = rm.nextInt(500);
        }
        for (int i = 0; i < 15000; i++){
            two[i] = rm.nextInt(500);
        }
        Arrays.sort(one);
        Arrays.sort(two);
        for (int i = 0; i < one.length; i++){
            Node node = new Node(one[i]);
            p1.next = node;
            p1 = p1.next;
        }
        for (int i = 0; i < two.length; i++){
            Node node = new Node(two[i]);
            p2.next = node;
            p2 = p2.next;
        }

        long start = System.nanoTime();
        Node re = change(headOne.next, headTwo.next);
        long end = System.nanoTime();

        System.out.println("直接操作:" + (end - start) + "ns");

        start = System.nanoTime ();
        Node reSet = changeSet(headOne.next, headTwo.next);
        end = System.nanoTime();
        System.out.println("Set时间:  " + (end - start) + "ns");
    }

    private static Node change(Node headOne, Node headTwo){
        //空值判断
        if (headTwo == null){
            return headOne;
        }
        if (headOne == null){
            return headTwo;
        }

        // Return the head node with the smaller first element, first take a node as the benchmark 
        Node reHead = headOne.data <= headTwo.data? headOne: headTwo;
        Node p2 = headOne.data >= headTwo.data? headTwo: headOne;     // another chain 
        Node pre = reHead;       // previous node 
        Node p1 = reHead.next;        // current chain

        while(p1 != null && p2 != null){
            if(p1.data <= p2.data){
                if (p1.data == p2.data){
                    p2 = p2.next;
                }
                if (p1.data != pre.data){
                    pre = pre.next;
                    p1 = p1.next;
                }else{
                    pre.next = p1.next;     // skip the same node 
                    p1 = p1.next;
                }
            }else{
                if (p2.data != pre.data){
                    pre.next = p2;
                    p2 = p2.next;
                    pre = pre.next;
                    pre.next = p1;
                }else {
                    p2 = p2.next;
                }
            }
        }

        Node now = p1 != null? p1: p2;
        pre.next = now;
        while(now!= null){
            if (pre.data == now.data){
                now = now.next;
                pre.next = now;
            }else{
                pre = pre.next;
                now = now.next;
            }
        }
        return reHead;
    }

    private static Node changeSet(Node headOne, Node headTwo){
        Node re = new Node(-1);
        Node head = re;
        TreeSet<Node> s = new TreeSet<>();
        while(headOne != null){
            s.add(headOne);
            headOne = headOne.next;
        }
        while(headTwo != null){
            s.add(headTwo);
            headTwo = headTwo.next;
        }
        Iterator iterator = s.iterator();
        while (iterator.hasNext()){
            Node p = (Node) iterator.next ();
            Node tmp = new Node(p.data);
            head.next = tmp;
            head = head.next;
        }
        return re.next;
    }
}

class Node implements Comparable{
    int data;
    Node next;

    public Node(int data){
        this.data = data;
    }

    @Override
    public  int compareTo(Object o) {
        Node t = (Node)o;
        if (this.data > t.data){
            return 1;
        }else if (this.data == t.data){
            return 0;
        }else {
            return -1;
        }
    }
}
implementation code

Then a screenshot of the running time:

 

 

If there are mistakes and deficiencies, I hope to correct
the journey of a thousand miles, starting with a single step

Reprinted in: https://www.cnblogs.com/ITyunbook/p/11521398.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326568918&siteId=291194637