博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode - 21. Merge two sorted lists
阅读量:5230 次
发布时间:2019-06-14

本文共 3022 字,大约阅读时间需要 10 分钟。

Description

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.

Difficulty

Solution

Idea

由于两个列表已排好序,因此可以设置两个下标i,j,分别指向l1和l2的首节点,再从头到尾分别比较两个列表当前下标对应的元素的大小关系,接着存到新列表(注意,这里题干要求“return it as a new list”)。需要留意两个列表的长度不一致的情况。

python

1 # Definition for singly-linked list. 2 # class ListNode(object): 3 #     def __init__(self, x): 4 #         self.val = x 5 #         self.next = None 6  7 class Solution(object): 8     def mergeTwoLists(self, l1, l2): 9         """10         :type l1: ListNode11         :type l2: ListNode12         :rtype: ListNode13         """14         if l1 == None and l2 == None:15             return None16 17         result = ListNode(-1)18         result_cur = result19 20         ptr1 = l121         ptr2 = l222 23         while ptr1 and ptr2:24             if ptr1.val <= ptr2.val:25                 new_node = ListNode(ptr1.val)26                 result_cur.next = new_node27                 ptr1 = ptr1.next28             else:29                 new_node = ListNode(ptr2.val)30                 result_cur.next = new_node31                 ptr2 = ptr2.next32 33             result_cur = result_cur.next34 35         while ptr1:36             new_node = ListNode(ptr1.val)37             result_cur.next = new_node38             result_cur = result_cur.next39             ptr1 = ptr1.next40 41         while ptr2:42             new_node = ListNode(ptr2.val)43             result_cur.next = new_node44             result_cur = result_cur.next45             ptr2 = ptr2.next46 47         return result.next
View Code

 

cpp

1 /** 2  * Definition for singly-linked list. 3  * struct ListNode { 4  *     int val; 5  *     ListNode *next; 6  *     ListNode(int x) : val(x), next(NULL) {} 7  * }; 8  */ 9 class Solution {10 public:11     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {12         if (l1==NULL && l2==NULL)13         {14             return NULL;15         }16         ListNode* ptr1 = l1;17         ListNode* ptr2 = l2;18         ListNode* result = new ListNode(-1);19         ListNode* result_cur = result;20 21         while (ptr1 && ptr2)22         {23             if (ptr1->val <= ptr2->val)24             {25                 result_cur->next = new ListNode(ptr1->val);;26                 ptr1 = ptr1->next;27             }else28             {29                 result_cur->next = new ListNode(ptr2->val);30                 ptr2 = ptr2->next;31             }32             result_cur = result_cur->next;33         }34         while (ptr1)35         {36             result_cur->next = new ListNode(ptr1->val);37             result_cur = result_cur->next;38             ptr1 = ptr1->next;39         }40         while (ptr2)41         {42             result_cur->next = new ListNode(ptr2->val);;43             result_cur = result_cur->next;44             ptr2 = ptr2->next;45         }46 47         return result->next;48     }49 };
View Code

 

 
 

转载于:https://www.cnblogs.com/gxcdream/p/7612561.html

你可能感兴趣的文章
NYOJ 120校园网络(有向图的强连通分量)(Kosaraju算法)
查看>>
SpringAop与AspectJ
查看>>
Leetcode 226: Invert Binary Tree
查看>>
http站点转https站点教程
查看>>
解决miner.start() 返回null
查看>>
bzoj 2007: [Noi2010]海拔【最小割+dijskstra】
查看>>
BZOJ 1001--[BeiJing2006]狼抓兔子(最短路&对偶图)
查看>>
C# Dynamic通用反序列化Json类型并遍历属性比较
查看>>
128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列
查看>>
定制jackson的自定义序列化(null值的处理)
查看>>
auth模块
查看>>
javascript keycode大全
查看>>
前台freemark获取后台的值
查看>>
log4j.properties的作用
查看>>
游戏偶感
查看>>
Leetcode: Unique Binary Search Trees II
查看>>
C++ FFLIB 之FFDB: 使用 Mysql&Sqlite 实现CRUD
查看>>
Spring-hibernate整合
查看>>
c++ map
查看>>
exit和return的区别
查看>>