8、合并两个有序的单链表

步骤
  • 1、同时遍历两个链表,并取出节点作比较,根据不同的情况生成新的链表
  • 2、若有链表为空,则直接遍历另一个链表即可
public class CombineLinked {

    private static Node head1;
    private static Node tail1;
    private static Node head2;
    private static Node tail2;
    private static Node head3;

    public static void main(String[] args) {
        String[] ar1 = {"1", "2", "3", "5", "18"};
        String[] ar2 = {"4", "6", "10"};
        for(int i = 0; i < ar1.length; i++) {
            createNode1(ar1[i]);
        }
        System.out.println("链表1:");
        printAll(head1);
        for(int i = 0; i < ar2.length; i++) {
            createNode2(ar2[i]);
        }
        System.out.println("链表2:");
        printAll(head2);
        System.out.println("合并后链表为:");
        combine(head1, head2);
        printAll(head3);
    }

    public static void combine(Node node1, Node node2) {
        Node tail = null;
        while(node1 != null && node2 != null) {
            if(Integer.valueOf(node1.data) <= Integer.valueOf(node2.data)) {
                if(head3 == null) {
                    head3 = node1;
                    tail = node1;
                } else {
                    tail.next = node1;
                    tail = node1;
                }
                node1 = node1.next;
            } else {
                if(head3 == null) {
                    head3 = node2;
                    tail = node2;
                } else {
                    tail.next = node2;
                    tail = node2;
                }
                node2 = node2.next;
            }
        }
        while(node1 != null) {
            if(head3 == null) {
                head3 = node1;
                tail = node1;
            } else {
                tail.next = node1;
                tail = node1;
            }
            node1 = node1.next;
        }
        while(node2 != null) {
            if(head3 == null) {
                head3 = node2;
                tail = node2;
            } else {
                tail.next = node2;
                tail = node2;
            }
            node2 = node2.next;
        }
    }
    public static void createNode1(String data) {
        Node node = new Node(data);
        if(head1 == null) {
            head1 = node;
            tail1 = node;
        } else {
            tail1.next = node;
            tail1 = node;
        }
    }
    public static void createNode2(String data) {
        Node node = new Node(data);
        if(head2 == null) {
            head2 = node;
            tail2 = node;
        } else {
            tail2.next = node;
            tail2 = node;
        }
    }

    public static void printAll(Node node) {
        while(node != null) {
            System.out.print(node.data + "-->");
            node = node.next;
        }
        System.out.println();
    }

    static class Node{
        private Node next;
        private String data;

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值