leetcode挨個兒刷150105(1):Merge Two Sorted Lists

隨着找工作的日期一天天的逼近,是時候在科研之餘寫一寫面試題中可能出現的代碼了,雖然說經常看開源框架的代碼,在面對這些簡短的題目時還是感覺有點兒難度,好在題目不多,索性從最簡單的做起來唄,加油!爭取每週做五個,每週末來個總結吧。

Long Way To Go!

No1:  https://oj.leetcode.com/submissions/detail/17943308/

Merge two sorted linked lists and return it as a new list. 

The new list should be made by splicing together the nodes of the first two lists.


<span style="font-size:18px;"> /* Definition for singly-linked list.*/
 #include <iostream>
 using namespace std;
 struct ListNode {
      int val;
     ListNode *next;
     ListNode(int x) : val(x), next(NULL) {}
  };
 </span>

解決方案:
<span style="font-size:18px;">class Solution {
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
        if( (l1 == NULL) && (l2 == NULL) )
            return NULL;    //寫完代碼反省的時候,發現冗餘了,可以不要。
        else if(l1 == NULL)
            return l2;
        else if(l2 == NULL)
            return l1;
        ListNode *p1;
        ListNode *p2;
        ListNode *l3=(l1->val < l2->val ? (p1=l1->next,p2=l2,l1) :(p2=l2->next,p1=l1,l2));
        ListNode *p3=l3;
    
        while( (p1 != NULL)  &&  ( p2 != NULL ))
        {
        	if(p1->val < p2->val)
       		{
        			p3->next = p1;
        			p3 = p3->next;
        			p1 = p1->next;
       		}
        	else
        	{
        		p3->next = p2;
        		p3 = p3->next;
        		p2 = p2->next;
        	}
        }
        
        if (p1 == NULL)
        	{
        		p3->next = p2;
        	}
        else if(p2 == NULL)
        	{
        		p3->next = p1;
        	}
        	return l3;
    }
    
};</span>

程序的測試部分:
<span style="font-size:18px;">int main()
{
	ListNode * l1= new ListNode(2);
	ListNode * l2 = new ListNode(1);
	ListNode *p=l1;
	ListNode *q=l2;
	for(int i = 1; i <= 0; i++)
	{
		ListNode *newnode=new ListNode(i);
		p->next=newnode;
		p=newnode;
		cout<<p->val<<endl;
	}
	for(int i = 2; i <= 1; i += 2)
	{
		ListNode *newnode=new ListNode(i);
		q->next=newnode;
		q=newnode;
		cout<<q->val<<endl;
	}
	Solution s;
	ListNode *l3 = s.mergeTwoLists(l1,l2);
	for(;l3!=NULL;l3=l3->next)
		cout<<l3->val;
	return 0;
}</span>


分析代碼,其時間複雜度爲O(min(m,n)),其中,m和n爲兩個鏈表的長度。



提交後,看到運行時間屬於較短的,可以接受。


但是總的說來,自己的代碼寫的很冗長,且不漂亮,看到《Leetcode題解》書中的範例程序如下:

<span style="font-size:18px;">//LeetCode, Merge Two Sorted Lists
// 時間複雜度 O(min(m,n)) ,空間複雜度 O(1)
class Solution {
   public:
      ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
             if (l1 == nullptr) return l2;
             if (l2 == nullptr) return l1;
             ListNode dummy(-1);
             ListNode *p = &dummy;
             for (; l1 != nullptr && l2 != nullptr; p = p->next) {
                     if (l1->val > l2->val) { p->next = l2; l2 = l2->next; }
                     else { p->next = l1; l1 = l1->next; }
               }
            p->next = l1 != nullptr ? l1 : l2;
            return dummy.next;
        }
};</span>

代碼比較精簡,但是多用了一個存儲空間,可對dummy進行改進,使p一開始就指向l1或者l2的頭。









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章