力扣练习题——合并两个有序链表1.0

合并两个有序链表,虽然成功了,但是内存消耗太大了。
欢迎各位大佬在下边评论。
采用的是一个全新的链表,然后将两个链表里边的数据一个一个的比较,然后放入新的链表里
这题用递归代码量比较少,就是我不熟练,可以在力扣里边找题解看看
在这里插入图片描述

package com.shengda.Demo0Likou;


import java.util.ArrayList;

public class Demo21 {
    public static void main(String[] args) {
        ArrayList<Integer> arr1 = new ArrayList<>();
        ArrayList<Integer> arr2 = new ArrayList<>();
        arr1.add(2);
        arr1.add(4);
        arr2.add(3);
        arr2.add(4);

        Solution so = new Solution();
        ListNode list1 = new ListNode(1);
        /*for (Integer integer : arr1) {
            ListNode list = new ListNode(integer);
            list.next = list1;
            list1 = list;

        }*/
        for (Integer integer : arr1) {
            ListNode temp = list1;
            ListNode list = new ListNode(integer);
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = list;
        }
        ListNode list2 = new ListNode(1);
        for (Integer integer : arr2) {
            // 创建一个辅助变量 temp
            ListNode list = new ListNode(integer);
            ListNode temp = list2;
            while (true) {
                // 找到链表的最后
                if (temp.next == null) {
                    break;
                }
                // 如果没有找到就将temp后移
                temp = temp.next;
            }
            // 当退出while循环时,表示temp就指向了链表的最后
            // 将最后这个节点的next指向新的节点
            temp.next =list;
        }
        so.mergeTwoLists(list1,list2);
    }

}

class Solution {
    public ListNode mergeTwoLists(ListNode  list1,ListNode list2) {
        ListNode list = new ListNode();
        if (list1 == null && list2 == null) {
            return list1;
        } else if (list1 != null && list2 == null) {
            return list1;
        } else if (list2 != null && list1 == null){
            return list2;
        }
        while (list1 != null && list2 != null) {
            ListNode data = new ListNode();
            if (list1.val > list2.val) {
                data.val = list2.val;
                addList(list,data);
                list2 = list2.next;
            } else {
                data.val = list1.val;
                addList(list,data);
                list1 = list1.next;
            }
        }
        if (list1 != null) {
            addList(list,list1);
        } else {
            addList(list,list2);
        }
        list = list.next;
        return list;
    }

    private void addList(ListNode list, ListNode data) {
        ListNode temp = list;
        while (temp.next != null) {
            temp = temp.next;
        }
        // 当退出while循环时,表示temp就指向了链表的最后
        // 将最后这个节点的next指向新的节点
        temp.next =data;
    }


}

class ListNode {
    int val;
    ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
        this.next = null;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public ListNode getNext() {
        return next;
    }

    public void setNext(ListNode next) {
        this.next = next;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值