LeetCode021MergeTwoSortedListssC语言
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.
题意:合并两个有序单链表,合并后的仍然是有序的。。。。。。。。。。。。。。。。。。。

创新互联建站-专业网站定制、快速模板网站建设、高性价比静海网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式静海网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖静海地区。费用合理售后完善,十多年实体公司更值得信赖。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
//首先判断有没有空链表的情况。。。。。
if(l1 && !l2)
return l1;
if(!l1 && l2)
return l2;
if(!l1 && !l2)
return NULL;
//还是和之前的002题要保存新链表头,中间节点head负责遍历
struct ListNode* head;
struct ListNode* ret;
//找到新链表的头
if(l1->valval){
head=l1;
l1=l1->next;
}else{
head=l2;
l2=l2->next;
}
ret=head;
//负责遍历。哪个小就指向哪个,直到有一个遍历完
while(l1&&l2){
if(l1->valval){
head->next=l1;
l1=l1->next;
}else{
head->next=l2;
l2=l2->next;
}
head=head->next;
}
//遍历完后看看谁还剩下直接指向剩下的部分
if(l1){
head->next=l1;
}
if(l2){
head->next=l2;
}
return ret;
} 。。。。。。。。。。。。。。。。太笨了。。。。。。。。。。。。。。。。继续练习吧少年。。。。。。。。。。。。。。。
名称栏目:LeetCode021MergeTwoSortedListssC语言
新闻来源:http://www.jxjierui.cn/article/iggipc.html


咨询
建站咨询
