合并两个有序的单向链表

1.首先定义一个单向链表结构

public class SingleLinkedList{

    private int size; //链表节点的个数
    private MyNode head;//头节点

    public SingleLinkedList(){
        size = 0;
        head = null;
    }

    //链表的每个节点类
    private class MyNode{
        private Object data;//每个节点的数据
        private MyNode next;//每个节点指向下一个节点的连接

        public MyNode(Object data){
            this.data = data;
        }
    }

    //在链表头添加元素
    public Object addHead(Object obj){
        MyNode newHead = new MyNode(obj);
        if(size == 0){
            head = newHead;
        }else{
            newHead.next = head;
            head = newHead;
        }
        size++;
        return obj;
    }

    //添加元素
    public void addValue(Object obj){
        MyNode addNode = new MyNode(obj);
        if(size == 0){
            head = addNode;
        }else{
            MyNode addNode1 = getEnd(this);
            addNode1.next = addNode;
        }
        size++;
    }

    //在链表头删除元素
    public Object deleteHead(){
        Object objc = head.data;
        head = head.next;
        size--;
        return objc;
    }

    //查找指定元素,找到了返回节点MyNode,找不到返回null
    private MyNode findObject(Object j){
        MyNode current = head;
        int tempSize = size;
        while(tempSize > 0){
            if(current.data == j){
                return current;
            }
            tempSize--;
            current = current.next;
        }
        return null;
    }

    //删除指定的元素,删除成功返回true
    private boolean delete(Object j){
        MyNode current = head;
        int tempSize = size;
        MyNode up = head;
        while(tempSize > 0){
            if(current.data == j){
                if(tempSize == size){
                    head = head.next;
                    size--;
                }else{
                    up.next = current.next;
                    size--;
                }
                return true;
            }
            tempSize--;
            up = current;
            current = current.next;
        }
        return false;
    }

//获取链表的最后一个元素
    private MyNode getEnd(SingleLinkedList list3){
        MyNode myNode = null;
        for(int i = 0; i < list3.size; i++){
            if(i == 0){
                myNode = head;
            }else{
                myNode = myNode.next;
            }
        }
        return myNode;
    }

    //打印链表所有元素
    public static void print(SingleLinkedList list){
        MyNode myNode = null;
        for(int i = 0; i < list.size; i++){
            if(i == 0){
                myNode = list.head;
            }else{
                myNode = myNode.next;
            }
            Log.e("SingleLinkedList", myNode.data.toString());
        }
    }

//合并两个有序链表,并生成新的链表
    public static void merge(SingleLinkedList list1, SingleLinkedList list2, SingleLinkedList list3){
        if(list2.head != null && (list1.head == null || (Integer) list1.head.data > (Integer) list2.head.data)){
            if(list3.head == null){
                list3.head = list2.head;
            }else{
                list3.getEnd(list3).next = list2.head;
            }
            list2.head = list2.head.next;
            list2.size--;
            list3.size++;
            merge(list1, list2, list3);
        }else if(list1.head != null){
            if(list3.head == null){
                list3.head = list1.head;
            }else{
                list3.getEnd(list3).next = list1.head;
            }
            list1.head = list1.head.next;
            list1.size--;
            list3.size++;
            merge(list1, list2, list3);
        }
    }
}

2.示例

try{
    SingleLinkedList singleLinkedList = new SingleLinkedList();
    singleLinkedList.addValue(1);
    singleLinkedList.addValue(4);
    singleLinkedList.addValue(7);
    singleLinkedList.addValue(9);
    SingleLinkedList singleLinkedList2 = new SingleLinkedList();
    singleLinkedList2.addValue(2);
    singleLinkedList2.addValue(5);
    singleLinkedList2.addValue(8);
    singleLinkedList2.addValue(10);
    SingleLinkedList singleLinkedList3 = new SingleLinkedList();
    SingleLinkedList.merge(singleLinkedList, singleLinkedList2, singleLinkedList3);
    SingleLinkedList.print(singleLinkedList3);
}catch( Exception e){
    Log.e("MainActivity", e.getMessage());
}
上一篇:Python深浅拷贝


下一篇:Python入门第二周day01