leetcode_效率题解_[python/C++]_21. Merge Two Sorted Lists(合并2个有序链表)

来源:互联网 发布:手机网络爸爸 编辑:程序博客网 时间:2024/04/30 07:41

相关题解:
leetcode_效率题解_23. Merge k Sorted Lists(合并k个有序链表)

题目链接
【题目】
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.
这里写图片描述
【分析】
解法1:

class Solution {public:    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {        if(l1 == NULL) return l2;        if(l2 == NULL) return l1;        ListNode* new_list = NULL;           if(l1->val < l2->val)                {               new_list = l1;                l1 = l1->next;            }            else                                  {                new_list = l2;                l2 = l2->next;          }         ListNode * pre = new_list;        while( l1 != NULL  && l2 != NULL ){            if( l1->val < l2->val ){                pre->next = l1;                l1 = l1->next;            }            else{                pre->next = l2;                l2 = l2->next;            }            pre = pre->next;        }        if( l1 ) pre->next = l1;        else pre->next = l2;        return new_list;    }};

解法2:
前面加一个无关的节点,不用去比较两个链表第一个的大小

class Solution {public:    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {        if(!l1 && !l2) return NULL;        ListNode * new_list = new ListNode(0);        ListNode * pre = new_list;        while( l1 != NULL  && l2 != NULL ){            if( l1->val < l2->val ){                pre->next = l1;                l1 = l1->next;            }            else{                pre->next = l2;                l2 = l2->next;            }            pre = pre->next;        }        if( l1 )  pre->next = l1;        else  pre->next = l2;        return new_list->next;    }};

解法3:
递归,leetcode上效率比较高

class Solution {  public:      ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {          if(l1 == NULL) return l2;          if(l2 == NULL) return l1;          if(l1->val < l2->val) {              l1->next = mergeTwoLists(l1->next, l2);              return l1;          } else {              l2->next = mergeTwoLists(l2->next, l1);              return l2;          }      }  };  

python:
解法1:非递归:

# Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution(object):    def mergeTwoLists(self, l1, l2):        """        :type l1: ListNode        :type l2: ListNode        :rtype: ListNode        """        if l1 is None and l2 is None:            return None        new_list = ListNode(0)        pre = new_list        while l1 is not None and l2 is not None:            if l1.val < l2.val:                pre.next = l1                l1 = l1.next            else:                pre.next = l2                l2 = l2.next            pre = pre.next        if l1 is not None:            pre.next = l1        else:            pre.next = l2        return new_list.next

解法2:递归:

def mergeTwoLists(self, l1, l2):      if not l1:          return l2      elif not l2:          return l1      else:          if l1.val <= l2.val:              l1.next = self.mergeTwoLists(l1.next, l2)              return l1          else:              l2.next = self.mergeTwoLists(l1, l2.next)              return l2 

解法3:
discuss上的一种解法,效率很高,但是我是不可能想到的

class Solution(object):    def mergeTwoLists(self, l1, l2):        """        :type l1: ListNode        :type l2: ListNode        :rtype: ListNode        """        new_list = ListNode(0)        new_list.next = l1        prev, head_of_1, head_of_2 = new_list, l1, l2        while head_of_2:            if not head_of_1:                prev.next = head_of_2                break            if head_of_1.val > head_of_2.val:                temp = head_of_2                head_of_2 = head_of_2.next                prev.next = temp                temp.next = head_of_1                prev = prev.next            else:                head_of_1, prev = head_of_1.next, head_of_1        return new_list.next
0 0
原创粉丝点击