首页 > 试题广场 >

(编程题)合并两个有序链表,保持链表顺序。例如:输入:链表1

[问答题]
(编程题)合并两个有序链表,保持链表顺序。
例如:
输入:
链表1: 1->3->5->7
链表2: 2->4->6->8
输出:
链表交集: 1->2->3->4->5->6->7->8
a = input().split('->')
b = input().split('->')
c = a+b
c.sort()
for i in c:
    if i == c[-1]:
        print(i)
    else:
        print(i+'->',end='')
python
发表于 2021-03-05 22:24:37 回复(0)
class Solution{
    public ListNode merge(ListNode l1,ListNode l2){
        if(l1==null){
            return l2;
        }
        if(l2==null){
            return l1;
        }
        if(l1.val<l2.val){
            l1.next = merge(l1.next,l2);
            return l1;
        }else{
            l2.next=merge(l1,l2.next);
            return l2;
        }
    }
}

发表于 2020-05-06 16:56:56 回复(0)
LinkedList merge(LinkedList list1,LinkedList list2){
    LinkedList dummy=new LinkedList();
    LinkedList rear=dummy;
    while(list1!=null && list2!=null){
       if(list1.val<list2.val){
            rear.next=list1;
            list1=list1.next;
            rear=rear.next;
        }else{
            rear.next=list2;
            list2=list2.next;
            rear=rear.next;
        }
    }
           if(list1!=null)rear.next=list1;
            rear.next=list2;
        return dummy.next;
}
发表于 2019-04-01 14:41:46 回复(0)
public static ListNode Merge(ListNode list1,ListNode list2){
        if(list1 == null && list2 == null){
            return null;
        }
        if(list1 == null){
            return list2;
        }
        if(list2 == null){
            return list1;
        }
        ListNode head = null;
        ListNode cur = null;
        while(list1 != null && list2 != null){
            if(list1.val <= list2.val){
                if(head == null){
                    head = list1;
                    cur = head;
                    list1 = list1.next;
                }else{
                    cur.next = list1;
                    cur = list1;
                    list1 = list1.next;
                }
            }else{
                if(head == null){
                    head = list2;
                    cur = head;
                    list2 = list2.next;
                }else{
                    cur.next = list2;
                    cur = cur.next;
                    list2 = list2.next;
                }
            }
        }
        if(list1 != null){
            cur.next = list1;
        }
        if(list2 != null){
            cur.next = list2;
        }
        return head;
    }
发表于 2018-08-30 09:26:38 回复(0)
public:
{
     ListNode * sortwo(ListNode *A, ListNode *B){
         ListNode * temp1=NULL,temp2=NULL,temp=NULL;
         
         ListNode * res;
         if(!A&&!B) return NULL;
         if(!A && B) return B;
         if(A&& !B) return A;
         if(A->val >= B->val)  {res=A; temp1=B->next;temp=B;temp2=A->next;}
         else{res=B; temp1=A->next;temp=A,temp2=B->next;}
         
         while(A&&B)
        {
            //temp1=temp1->next;
            if(temp->val>=temp1->val)
            {   if(temp->val<=temp1->val)
                {
                  res->next=temp;
                  res->next->next=temp1;
                  temp=temp2;
                  temp2=temp2->next;
                }
                else
                {
                       
                }
            temp1=temp1->next;
            
        }
}
};

发表于 2018-04-07 22:38:59 回复(2)