摘要:题目将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。下面我们就分别给出两端优化的代码先取一个小的做头节点给一个哨兵位的头节点,方便尾插,处理完以后,再删掉。
将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
解决这个问题的方法是,可以将两个链表中的数据依次比较,然后我们可以将较小的取下来尾插,就这样依次进行,直到其中一个走到空,就可以结束了。
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){ if(l1 == NULL) return l2; if(l2 == NULL) return l1; struct ListNode*n1,*n2; n1 = l1; n2 = l2; struct ListNode*newhead,*tail; newhead = tail = NULL; while(n1 && n2) { if(n1->val<n2->val) { if(tail == NULL) { newhead = tail = n1; } else { tail->next = n1; tail = n1; } n1 = n1->next; } else { if(tail == NULL) { newhead = tail = n2; } else { tail->next = n2; tail = n2; } n2 = n2->next; } } if(n1) { tail->next = n1; } if(n2) { tail->next = n2; } return newhead;}
看上面的代码,我们可能会发现其中的循环有点冗余,因此我们可以改进一下,我们可以在这两个链表中首先比较找出一个较小的值作为头节点,然后进行尾插;我们还可以用一个带哨兵位的头节点来解决这个问题。下面我们就分别给出两端优化的代码:
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){ if(l1 == NULL) return l2; if(l2 == NULL) return l1; struct ListNode*n1,*n2; n1 = l1; n2 = l2; struct ListNode*newhead,*tail; newhead = tail = NULL; //先取一个小的做头节点 if(n1->val < n2->val) { newhead = tail = n1; n1 = n1->next; } else { newhead = tail = n2; n2 = n2->next; } while(n1 && n2) { if(n1->val<n2->val) { tail->next = n1; tail = n1; n1 = n1->next; } else { tail->next = n2; tail = n2; n2 = n2->next; } } if(n1) { tail->next = n1; } if(n2) { tail->next = n2; } return newhead;}
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){ if(l1 == NULL) return l2; if(l2 == NULL) return l1; struct ListNode*n1,*n2; n1 = l1; n2 = l2; struct ListNode*newhead,*tail; newhead = tail = NULL; //给一个哨兵位的头节点,方便尾插,处理完以后,再删掉。 newhead = tail = (struct ListNode*)malloc(sizeof(struct ListNode)); while(n1 && n2) { if(n1->val<n2->val) { tail->next = n1; tail = n1; n1 = n1->next; } else { tail->next = n2; tail = n2; n2 = n2->next; } } if(n1) { tail->next = n1; } if(n2) { tail->next = n2; } struct ListNode* first = newhead->next; free(newhead); return first;}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/119939.html
摘要:题目地址题目描述将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。若两者其中有一个为就返回另一个。 题目地址:https://leetcode-cn.com/probl...题目描述:将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 解答:递归思想。若两者其中有一个为null就返回另一个。否则,val...
摘要:同时让当前结点的指针指向,从而实现从链表尾部开始的局部反转当递归函数全部出栈后,链表反转完成。这个链表的倒数第个节点是值为的节点。新链表是通过拼接给定的两个链表的所有节点组成的。 目录 1.反转链表 K个一组翻转链表 链表的中间节点: 链表倒数第k个节点: 删除链表倒数第k个节...
摘要:示例输入输出示例输入输出示例输入输出提示两个链表的节点数目范围是和均按非递减顺序排列递归法分析递归法,和之前的一样,还是需要先设置跳出判断,这里设置为空的时候跳出。 ...
摘要:新链表是通过拼接给定的两个链表的所有节点组成的。保证链表长度小于等于。注意,函数返回结果后,链表必须保持其原始结构。注意不作为参数进行传递,仅仅是为了标识链表的实际情况。如果是,则在该链表中没有环。说明不允许修改给定的链表。 ...
摘要:三代码实现用空格分割链表每个元素将链表分为组组组内翻转,如果不够一组,就不进行翻转。下一组的起始位置为什么的上限是输入的要求是每个元素后面跟个但是最后一个没有,最后一个元素就要最后输出。 ...
阅读 1813·2021-11-18 10:02
阅读 878·2021-11-17 09:33
阅读 1577·2021-09-14 17:57
阅读 2606·2021-09-04 16:41
阅读 938·2019-08-30 15:55
阅读 1254·2019-08-29 17:27
阅读 877·2019-08-29 17:12
阅读 2347·2019-08-29 15:38