Merge Two Sorted Lists LeetCode
生活随笔
收集整理的這篇文章主要介紹了
Merge Two Sorted Lists LeetCode
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
---恢復(fù)內(nèi)容開始---
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. 合并兩個(gè)排好序的鏈表
?
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 if(NULL!=l1) return l1; 15 else return l2; 16 } 17 else 18 { 19 ListNode *head; 20 if(l1->val<=l2->val) head=l1; 21 else head=l2; 22 ListNode *up=head; 23 ListNode *list1=l1; 24 ListNode *list2=l2; 25 while(list1!=NULL&&list2!=NULL) 26 { 27 ListNode *temp; 28 if(list1->val<=list2->val) 29 { 30 temp=list1; 31 list1=list1->next; 32 } 33 else 34 { 35 temp=list2; 36 list2=list2->next; 37 } 38 up->next=temp; 39 up=temp; 40 } 41 if(list1!=NULL||list2!=NULL) 42 { 43 if(list1!=NULL) 44 { 45 up->next=list1; 46 } 47 else up->next=list2; 48 } 49 return head; 50 } 51 } 52 };?
轉(zhuǎn)載于:https://www.cnblogs.com/sqxw/p/3947752.html
總結(jié)
以上是生活随笔為你收集整理的Merge Two Sorted Lists LeetCode的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android升级SDK后,XML gr
- 下一篇: LVS之NAT模型配置实验