两个有序单链表的合并

public class InsertList {

    public Node Insert(Node headNode1, Node headNode2) {
        Node headNode = null;
        Node base = new Node(headNode1);
        Node step = new Node(headNode2);
        if (base == null && step != null) {
            headNode = headNode;
        } else if (step == null && base != null) {
            headNode = headNode;
        } else if (base != null && step != null) {
            Node prebase = null;
            while (step != null) {
                while (base != null) {
                    if (base.data >= step.data) {
                        Node insertNode= new Node(step.data);
                        if (prebase == null) {
                            insertNode.next = base;
                            headNode=insertNode;
                        } else {
                            prebase.next = insertNode;
                            insertNode.next = base;
                        }
                        break;
                    }
                    prebase = base;
                    base = base.next;
                }

                step = step.next;
            }
        }
        return headNode;
    }

}
View Code

猜你喜欢

转载自www.cnblogs.com/zecdllg/p/9767056.html
今日推荐