[LeetCode-21] Merge Two Sorted Lists(合并两个有序链表)
来源:互联网 发布:linux命令行编辑文件 编辑:程序博客网 时间:2024/05/21 14:08
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* FirstLists, struct ListNode* SecondLists){ /*1.异常处理*/ if(!FirstLists && !SecondLists) return NULL; else if(!FirstLists && SecondLists) return SecondLists; else if(FirstLists && !SecondLists) return FirstLists; struct ListNode *FirstListsTemp = FirstLists; struct ListNode *SecondListsTemp = SecondLists; struct ListNode *head = NULL; struct ListNode *curNode = NULL; /*2.合并两个有序的链表*/ while(FirstListsTemp&&SecondListsTemp) { /*3.获得头结点,作为返回值*/ if(head == NULL) { if(FirstListsTemp->val >= SecondListsTemp->val) { curNode = SecondListsTemp; head = curNode; SecondListsTemp = SecondListsTemp->next; curNode->next = NULL; } else{ curNode = FirstListsTemp; head = curNode; FirstListsTemp = FirstListsTemp->next; curNode->next = NULL; } } else { if(FirstListsTemp->val >= SecondListsTemp->val) { curNode->next = SecondListsTemp; curNode = curNode->next; SecondListsTemp = SecondListsTemp->next; curNode->next = NULL; } else{ curNode->next = FirstListsTemp; curNode = curNode->next; FirstListsTemp = FirstListsTemp->next; curNode->next = NULL; } } } /*4.剩下的有序链表*/ if(FirstListsTemp) { curNode->next = FirstListsTemp; } if(SecondListsTemp) { curNode->next = SecondListsTemp; } return head;}
0 0
- Merge Two Sorted Lists 合并两个有序链表@LeetCode
- [LeetCode]Merge Two Sorted Lists-合并两个有序链表
- Leetcode Merge Two Sorted Lists 合并两个有序链表
- leetcode Merge Two Sorted Lists 合并两个有序链表
- Leetcode Merge Two Sorted Lists(合并两个有序表)
- [LeetCode-21] Merge Two Sorted Lists(合并两个有序链表)
- Merge Two Sorted Lists(合并两个有序链表)
- LeetCode-21 Merge Two Sorted Lists(合并两个有序链表)
- LeetCode OJ 之 Merge Two Sorted Lists(合并两个有序的链表)
- LeetCode Merge Two Sorted Lists(合并两个有序链表)
- LeetCode | Merge Two Sorted Lists(合并两个链表)
- 两个有序链表的合并 Merge Two Sorted Lists
- Merge Two Sorted Lists 合并两个有序链表
- Merge Two Sorted Lists, 合并两个有序链表
- LeetCode(Merge Two Sorted Lists ) 合并两个有序的链表
- LeetCode--Merge Two Sorted Lists(合并两个有序链表)Python
- Leetcode:Merge Two Sorted Lists 合并两个有序单链表
- (LeetCode)Merge Two Sorted Lists --- 合并两个有序序列
- 配置Struts2项目
- TextView 显示html文本
- 免费MATLAB替代品winpython
- java中的异常(Exception)介绍
- 出自ios深入浅出专栏(内购)
- [LeetCode-21] Merge Two Sorted Lists(合并两个有序链表)
- instanceof
- 教你如何减少家用电器的辐射
- 顺序链表类模板的实现
- 16进制
- Android开发岗面试之常用面试题
- ubuntu 操作系统的目录结构
- ASO基础知识介绍
- Android SDK无法下载,ADT官网无法打开解决方法