leetcode[21]:Merge Two Sorted Lists
2015-06-07 17:31
309 查看
Merge Two Sorted Lists
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.
思路比较混乱,代码也比较乱。。。大概是以list 1 为基准,如果l2的数较小,直接移动l1的头指针,如果l1前面的较小便移动指针将l2的数值穿插进去。
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.
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { struct ListNode *L, *tmp; if(l1 == NULL ) return l2; if(l2 == NULL ) return l1; L=l1; while(1) { if(L->val > l2->val) { tmp = ( struct ListNode* ) malloc( sizeof( struct ListNode* ) ); tmp->val = l2->val; tmp->next = l1; l1 = tmp; L=l1; l2 = l2->next; } else { if( L->next == NULL ) { L->next=l2; return l1; } if( L->next->val < l2->val) { L = L->next; } else { tmp = ( struct ListNode* ) malloc( sizeof( struct ListNode* ) ); tmp->val = l2->val; tmp->next = L->next; L->next = tmp; l2 = l2->next; L = L->next; } } if (l2 == NULL) break; } return l1; }
思路比较混乱,代码也比较乱。。。大概是以list 1 为基准,如果l2的数较小,直接移动l1的头指针,如果l1前面的较小便移动指针将l2的数值穿插进去。
相关文章推荐
- C#中Ilist与list的区别小结
- C#中IList<T>与List<T>的区别深入解析
- C#对list列表进行随机排序的方法
- C++实现的泛型List类分享
- C# Datagridview绑定List方法代码
- javascript radio list的实现细节(多浏览器兼容)
- c++ STL容器总结之:vertor与list的应用
- 大家注意vector, list, set, map成员函数erase
- ASP.NET―001:GridView绑定List、页面返回值具体实现
- List all the Databases on a SQL Server
- js Map List 遍历使用示例
- Java List双击事件实现方法
- velocity显示List与Map的方法详细解析
- 关于STL中list容器的一些总结
- 关于C#泛型列表List<T>的基本用法总结
- Java集合类中文介绍
- use jscript with List Proxy Server Information
- list泛型自定义排序示例
- C#中IEnumerable、ICollection、IList、List之间的区别
- Perl List::Util模块使用实例