LeetCode 之 Merge Two Sorted Lists
2015-11-23 22:08
316 查看
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.
这个挺简单的,关键在于条件判断和链表指针操作,代码如下:
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* head; if(l1==NULL) return l2; if(l2==NULL) return l1; ListNode* l11=l1; ListNode* l22=l2; if(l11->val>=l22->val){ head=l22; l22=l22->next; }else{ head=l11; l11=l11->next; } ListNode* last=head; while(l11!=NULL||l22!=NULL){ if(l11==NULL&&l22!=NULL){ last->next=l22; return head; }else if(l11!=NULL&&l22==NULL){ last->next=l11; return head; }else if(l11->val>l22->val){ last->next=l22; last=last->next; l22=l22->next; last->next=NULL; }else{ last->next=l11; last=last->next; l11=l11->next; last->next=NULL; } } return head; } };
相关文章推荐
- 【C++】非递归求二叉树的前序和中序序列
- 南大软院大神养成计划--HTML和CSS基础课程(四)
- 提问的智慧
- 集合相关知识
- 使用(x&y) + ((x^y)>>1) 求平均数
- DICOM:DICOM3.0网络通信协议(三)
- mybatis 一对多
- java集合类ArrayList
- Poj 352 UVA oj 1395 Slim Span 最小生成树应用
- 很实用的linux 上的svn安装和svnserver 的重启
- 没有躲过的坑--正则表达式截取字符串
- Codeforces 599C Day at the Beach 【思维】
- Linux下编译opencv
- Android第三方开源水面波浪波形view:WaveView(电量、能量、容量指示)
- 控制流
- 我的彩票界面
- 没有躲过的坑--正则表达式截取字符串
- 非主流自然语言处理——遗忘算法系列(二):大规模语料词库生成
- [转]iOS isa 浅析
- 内存对齐规则