Day008:merge two sorted lists
来源:互联网 发布:wcf 数据库 编辑:程序博客网 时间:2024/05/29 19:27
Problem:
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; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode *n1=l1,*n2=l2,*head=nullptr,*node=nullptr; if(n1==nullptr||n2==nullptr) return (n1==nullptr?n2:n1); if(n1->val>n2->val){ head=n2; n2=n2->next; } else{ head=n1; n1=n1->next; } node=head; while(n1!=nullptr && n2!=nullptr){ if(n1->val>n2->val){ node->next=n2; n2=n2->next; } else{ node->next=n1; n1=n1->next; } node=node->next; } if(n1!=nullptr){ node->next=n1; } if(n2!=nullptr){ node->next=n2; } return head; }};
0 0
- Day008:merge two sorted lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- 2017.03.16_iterator、EnhancedFor及Set
- JSP声明语句/脚本段/表达式
- 关于QCustomPlot的绘图
- 【变量过滤】防止代码注入
- java:求一个字符串中子字符串的个数,简便算法
- Day008:merge two sorted lists
- android tree树状列表+checkbox
- 迷茫<第一篇:初到北京>
- [转]Facebook 开源的快速文本分类器 FastText
- iOS 刷新一个行
- 2016级数据结构第一次上机解题报告
- java.io.IOException: No error in nativeavailable
- 频繁模式挖掘 Apri算法
- acm程序设计书中题目第e题解析