[C语言][LeetCode][21]Merge Two Sorted Lists
来源:互联网 发布:彩票娱乐系统源码 编辑:程序博客网 时间:2024/04/29 18:32
题目
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.
标签
Linked List
难度
简单
分析
题目意思是合并两个排好序的链表,并返回新的链表。解题思路是先找到链表的头,如果L1的第一个元素比较小,新链表的头结点就指向L1,如果L2的第一个元素比较小,新链表的头结点就指向L2。剩下的就是逐个比较剩余L1和L2里的元素。需要注意的是,最后是否比较结束了。
C代码实现
struct ListNode *head; struct ListNode *ret; if(!l1 && l2) return l2; if(l1&&!l2) return l1; if(!l1 && !l2) return NULL; if(l1->val <= l2->val) { head = l1; l1 = l1->next; } else { head = l2; l2 = l2->next; } ret = head; while(l1 && l2) { if(l1->val < l2->val) { head->next = l1; l1 = l1->next; } else { head->next = l2; l2 = l2->next; } head->next->next = NULL; head = head->next; } if(l1) head->next = l1; else if(l2) head->next = l2; return ret;
0 0
- Leetcode c语言- Merge Two Sorted Lists
- [C语言][LeetCode][21]Merge Two Sorted Lists
- LeetCode-21-Merge Two Sorted Lists(C语言实现)
- 【leetcode c++】21 Merge Two Sorted Lists
- 【LeetCode-21】 Merge Two Sorted Lists(C++)
- 【LeetCode】21. Merge Two Sorted Lists C语言
- LeetCode 21 Merge Two Sorted Lists (C,C++,Java,Python)
- [C++]LeetCode: 13 Merge Two Sorted Lists
- Leetcode 21. Merge Two Sorted Lists(C++)
- 【C++】【LeetCode】21. Merge Two Sorted Lists
- LeetCode(21)Merge Two Sorted Lists
- [LeetCode 21] Merge Two Sorted Lists Solution
- [leetcode 21] Merge Two Sorted Lists
- LeetCode 21:Merge Two Sorted Lists
- LeetCode | #21 Merge Two Sorted Lists
- [Leetcode] 21 - Merge Two Sorted Lists
- leetcode.21------------Merge Two Sorted Lists
- [leetcode]21 Merge Two Sorted Lists
- 白话Spring(基础篇)---AOP(实例篇-1)
- 算法补习-第四天-查找(上)
- Java线程详解
- hadoop伪分布式安装步骤
- RTTI
- [C语言][LeetCode][21]Merge Two Sorted Lists
- Java 封装
- 51Nod 1413 权势二进制(巧妙,贪心)
- socket.io笔记[转载]
- 将磁盘的FAT32格式转换为NTFS
- 技术学习曲线安排
- Flex 布局教程:语法篇
- Mysql 5.7.11 安装在 win10,服务启动失败解决方法
- Delphi历史版本介绍(一)从Delphi1到Delphi7