[LeetCode Likou Jianzhi Offer 25] Combine two sorted linked lists, enter two ascending linked lists, merge the two linked lists and make the nodes in the new linked list still in ascending order.

learning target:

Goal: proficiently use the knowledge learned in Java


Learning Content:

The content of this article: Implemented in Java: Merge two sorted linked lists


Title description:

Enter two linked lists in ascending order, merge the two linked lists and make the nodes in the new linked list still in ascending order.

Example 1:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

Problem-solving ideas:

Combine the two sorted linked lists into a new sorted linked list. Since the original linked list is ordered, we only need to traverse in order, compare the size of each data in the two linked lists, sort them, and put them in the new linked list. When the data is the same, just put a value at will

  • First create a new linked list that saves the new sorted linked list
        ListNode cur1=l1;//用于遍历l1链表
        ListNode cur2=l2;//用于遍历l2链表
        ListNode newHead=new ListNode(0);//创建带傀儡的新链表,用于存放新的排序链表
        ListNode newTail=newHead;//记录尾结点位置
  • Exclude the case where a certain linked list is empty
 //判断两个链表是否都是空链表
       if(cur1==null&&cur2==null){
    
    
           return null;
       }
       //如果cur1为空,则返回l2链表
       if(cur1==null){
    
    
           return l2;
       }
       //如果cur2为空,则返回l1链表
       if(cur2==null){
    
    
           return l1;
       }
  • Compare the size of two linked list data
   while(cur1!=null&&cur2!=null){
    
    
            //链表1的数据小于链表2的数据
            if(cur1.val<cur2.val){
    
    
                newTail.next=cur1;//将较小数据存放到新链表
                cur1=cur1.next;//更新链表位置
            }
            //链表1的数据大于等于链表2的数据
            else{
    
    
                newTail.next=cur2;//将较小数据存放到新链表
                cur2=cur2.next;//更新链表位置
            }
            newTail=newTail.next;
        }
  • After the traversal is over, determine whether there is a linked list that has not been traversed
     //遍历结束之后,判断是否有某一个链表没有遍历完
        if(cur1==null){
    
    
            newTail.next=cur2;
        }else{
    
    
            newTail.next=cur1;
        }

Implementation code

 public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    
    
        ListNode cur1=l1;
        ListNode cur2=l2;
        ListNode newHead=new ListNode(0);//创建带傀儡的新链表,用于存放新的排序链表
        ListNode newTail=newHead;//记录尾结点位置
        //判断两个链表是否都是空链表
        if(cur1==null&&cur2==null){
    
    
            return null;
        }
        //如果cur1为空,则返回l2链表
        if(cur1==null){
    
    
            return l2;
        }
        //如果cur2为空,则返回l1链表
        if(cur2==null){
    
    
            return l1;
        }
        //比较两个链表数据的大小
        while(cur1!=null&&cur2!=null){
    
    
            //链表1的数据小于链表2的数据
            if(cur1.val<cur2.val){
    
    
                newTail.next=cur1;//将较小数据存放到新链表
                cur1=cur1.next;//更新链表位置
            }
            //链表1的数据大于等于链表2的数据
            else{
    
    
                newTail.next=cur2;//将较小数据存放到新链表
                cur2=cur2.next;//更新链表位置
            }
            newTail=newTail.next;
        }
        //遍历结束之后,判断是否有某一个链表没有遍历完
        if(cur1==null){
    
    
            newTail.next=cur2;
        }else{
    
    
            newTail.next=cur1;
        }
        return newHead.next;
    }

Guess you like

Origin blog.csdn.net/zhangxxin/article/details/114500337