栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

package com.shengda.Demo0Likou;


import java.util.ArrayList;

public class Demo21 {
    public static void main(String[] args) {
        ArrayList arr1 = new ArrayList<>();
        ArrayList 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 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;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/732903.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号