数据结构与算法(四)链表(五)- 两个有序链表合并+寻找中间节点

public class CombineLinkedList {

    @Test
    public void test() {
        System.out.println("------------------------------");
        CombineLinkedList list = new CombineLinkedList();
        CombineLinkedList target = new CombineLinkedList();
        list.insert(0);
        list.insert(1);
        list.insert(3);
        list.insert(5);
        list.insert(7);
        list.insert(9);
        list.insert(10);
        list.insert(11);
        target.insert(2);
        target.insert(4);
        target.insert(6);
        target.insert(8);
        target.insert(10);
        target.show();
        System.out.println("------------------------------");
        System.out.println("------------------------------ list中间节点的值是" + list.findCenter());
        System.out.println("------------------------------ target中间节点的值是" + target.findCenter());
        System.out.println("------------------------------");
        Node current = list.head;
        while (current != null) {
            target.insertSorted(current.data);
            current = current.next;
        }
        target.show();
        System.out.println("------------------------------");
    }

    class Node {
        int data;
        Node next;

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

    Node head = null;
    int size = 0;

    public int insert(int data) {
        Node temp = new Node(data);
        if (head == null) {
            head = temp;
            return ++size;
        }
        Node current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = temp;
        return ++size;
    }

    public int insertSorted(int data) {
        Node temp = new Node(data);
        if (head == null) {
            head = temp;
            return ++size;
        }
        Node current = head;
        if (data < head.data) {
            //插到头结点前面
            temp.next = head;
            head = temp;
            return ++size;
        }
        while (current.next != null) {
            if (temp.data >= current.data && temp.data <= current.next.data) {
                //当大于等于当前节点并小于等于下一个节点时就插在这两个节点中间
                temp.next = current.next;
                current.next = temp;
                return ++size;
            }
            //list没有比当前值更大的节点,插在末尾
            current = current.next;
        }
        current.next = temp;
        return ++size;
    }

    public void show() {
        Node current = head;
        while (current != null) {
            System.out.println("" + current.data);
            current = current.next;
        }
    }

    public int findCenter() {
        int center;
        if (size % 2 == 0) {
            //偶数情况,要的是中间点前面那个数字
            center = size / 2 - 1;
        } else {
            //奇数情况
            center = size / 2;
        }


        Node current = head;
        for (int i = 0; i < center; i++) {
            current = current.next;
        }

        return current.data;
    }
}
发布了26 篇原创文章 · 获赞 39 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/u012185875/article/details/105074147
今日推荐