数据结构 合并两个链表 力扣

这篇文章提供了一个Java类Solution,用于合并两个已排序的链表,通过比较节点值并按顺序连接它们。函数`mergeTwoLists`处理了空列表和非空列表的各种情况。
摘要由CSDN通过智能技术生成

21. Merge Two Sorted Lists

You are given the heads of two sorted linked lists list1 and list2.

Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.

Return the head of the merged linked list.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    private ListNode l3 = new ListNode();
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        
        if(isEmpty(list1)&&isEmpty(list2))
          return null;
        else if(isEmpty(list1)&&!isEmpty(list2))
          return list2;
        else if(!isEmpty(list1)&&isEmpty(list2))
          return list1;
        else {

            int l1 = 0,i = 0;
            while(!isEmpty(list1)&&!isEmpty(list2)){
                
                if(i%2==0){
                if(isGreat(list1.val,list2.val)){
                merge(list1.val);
                list1 = list1.next;
                l1 = 1;
                }else{
                merge(list2.val);
                list2 = list2.next;
                l1 = 0;
                }
             }else
             {
                if(l1==1){
                merge(list2.val);
                list2 = list2.next;
                l1 = 0;
                }else
                {
                merge(list1.val);
                list1 = list1.next; 
                }

             }
            

            if(isEmpty(list1)&&!isEmpty(list2)){
                while(!isEmpty(list2)){
                merge(list2.val);
                list2 = list2.next;
                }
            }else if(!isEmpty(list1)&&isEmpty(list2)){
                while(!isEmpty(list1)){
                merge(list1.val);
                list1 = list1.next;
                }
            }

    }
      
        }

    return l3=l3.next;
    }

    public boolean isGreat(int a,int b){
        return a<=b;
    }

    public void merge(int val){
        ListNode newNode = new ListNode();
        newNode.val = val;
        newNode.next = null;
        if(l3==null){
            l3 = newNode;
        }else
        {
            ListNode tmp = l3;
            while(tmp.next!=null){
                tmp = tmp.next;
            }
            tmp.next = newNode;
        }
    }


    public boolean isEmpty(ListNode l){
        return l==null;
    }
}

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值