Java 实现合并两个已排序的链表

两个单调递增的链表,将两个链表合并为一个单调不减的链表。

代码

    static class Node {
        int val;
        Node next;

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

    /**
     * 输出链表
     * @param head
     */
    private static void printNodeList(Node head) {
        if (head == null) {
            return;
        }
        Node _node = head;
        while (_node != null) {
            System.out.print(_node.val);
            _node = _node.next;
            if (_node != null) {
                System.out.print(", ");
            }
        }
        System.out.println();
    }

    /**
     * 递归
     * @param head1
     * @param head2
     * @return
     */
    private static Node merge(Node head1, Node head2) {
        // 递归终止条件
        // head1为null,则返回head2
        // head2为null,则返回head1
        if (head1 == null) {
            return head2;
        } else if (head2 == null) {
            return head1;
        }
        Node mergeHead = null;
        // 如果head1的值小于head2的值,则合并后的链表head为head1,然后使用head1.next作为新的头节点和当前head2作为参数继续调用方法
        // 否则为head2,然后使用head2.next作为新的头节点和当前head1作为参数继续调用方法
        if (head1.val <= head2.val) {
            mergeHead = head1;
            mergeHead.next = merge(head1.next, head2);
        } else {
            mergeHead = head2;
            mergeHead.next = merge(head1, head2.next);
        }
        return mergeHead;
    }

    /**
     * 非递归
     * @param head1
     * @param head2
     * @return
     */
    private static Node merge2(Node head1, Node head2) {
        // head1与head2任一为null,则无需合并,直接返回另一个即可
        if (head1 == null) {
            return head2;
        } else if (head2 == null) {
            return head1;
        }
        // 合并后的链表,初始化值随意
        Node mergeHead = new Node(0);
        // 临时节点,用于移动合并链表的指针
        Node temp = mergeHead;
        while (head1 != null && head2 != null) {
            // 如果head1的值小于head2的值,则合并后的链表当前节点的next为head1,并且head1指向其next
            // 否则为head2,并且head2指向其next
            if (head1.val <= head2.val) {
                temp.next = head1;
                head1 = head1.next;
            } else {
                temp.next = head2;
                head2 = head2.next;
            }
            // 移动指针
            temp = temp.next;
        }
        // 如果head1链表不为null,则直接放到合并链表的尾部
        if (head1 != null) {
            temp.next = head1;
        }
        // 如果head2链表不为null,则直接放到合并链表的尾部
        if (head2 != null) {
            temp.next = head2;
        }
        // 因为合并链表的头节点是随意初始化的,所以真正合并后的链表头节点应该是当前头节点的next
        return mergeHead.next;
    }

    public static void main(String[] args) {
        Node node = new Node(1);
        Node node2 = new Node(3);
        node.next = node2;
        Node node3 = new Node(7);
        node2.next = node3;
        Node node4 = new Node(13);
        node3.next = node4;

        Node _node = new Node(4);
        Node _node2 = new Node(6);
        _node.next = _node2;
        Node _node3 = new Node(10);
        _node2.next = _node3;
        Node _node4 = new Node(18);
        _node3.next = _node4;
//        Node merge = merge(node, _node);
        Node merge = merge2(node, _node);
        printNodeList(merge);
    }

你可能感兴趣的:(剑指offer算法,剑指offer算法(Java))