1. 题目描述
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
2.代码如下
//2018-11-17-20:22
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
//方法一:直接将两个链表连接起来,再用冒泡排序就可以了
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
typedef struct ListNode list;
list *p1 = l1;
list *p2 = l2;
int size = 0;
if(p1==NULL)//要考虑到p1、p2为null的情况
{
return p2;
}
if(p2==NULL)
{
return p1;
}
while(p1->next!=NULL)
{
p1 = p1->next;
}//现在p1处于l1的最后一个结点上
p1->next = p2;//顺利将两个链表串联在一块
p1 = l1;//p1回到起始之处
while(p1!=NULL)
{
size++;
p1 = p1->next;
}
p1 = l1;
int *arrary = malloc(sizeof(int)*size);
memset(arrary,0,sizeof(int)*size);
for(int i = 0;i<size;i++)
{
arrary[i] = p1->val;
p1 = p1->next;
}//得到包含这条链表的所有数的数组
//开始冒泡排序
for(int i = 0;i<size;i++)
{
for(int j = i +1;j<size;j++)
{
if(arrary[i]>arrary[j])
{
int temp = arrary[i];
arrary[i] = arrary[j];
arrary[j] = temp;
}
}
}
p1 = l1;
int i = 0;
while(p1!=NULL&&i<size)
{
p1->val = arrary[i++];
p1 = p1->next;
}
return l1;
}
3.新解法
2020-03-07 20:38
1.之前的解法过于暴力,现使用的方法是,分别逐结点遍历和比较两条链表,将较小的结点尾插入一条新链表中,最终返回新链表即可。
2.我使用一个初始为空指针的pnewlist代表新链表的插入位置,这就要求,插入结点的时候要判断该待插入结点是否是第一个元素,是的话pnewlist直接指向第一个结点,不是的话说明新链表已经有了元素了,可以直接尾插为新链表中的最后一个结点。
#include <iostream>
#include <vector>
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
if (not list1) {
return list2;
}
else if (not list2) {
return list1;
}
ListNode* pHead1 = list1;
ListNode* pHead2 = list2;
ListNode* pLastNode = nullptr;
ListNode* pRealHead = nullptr;
while (pHead1 && pHead2) {
if (pHead1->val <= pHead2->val)
{
if (pLastNode == nullptr) {
ListNode* ptmep = pHead1->next;
pLastNode = pHead1;
pHead1->next = nullptr;
pHead1 = ptmep;
pRealHead = pLastNode;
}
else {
ListNode* ptmep = pHead1->next;
pLastNode->next = pHead1;
pHead1->next = nullptr;
pHead1 = ptmep;
pLastNode = pLastNode->next;
}
}
else
{
if (pLastNode == nullptr) {
ListNode* ptmep = pHead2->next;
pLastNode = pHead2;
pHead2->next = nullptr;
pHead2 = ptmep;
pRealHead = pLastNode;
}
else {
ListNode* ptmep = pHead2->next;
pLastNode->next = pHead2;
pHead2->next = nullptr;
pHead2 = ptmep;
pLastNode = pLastNode->next;
}
}
}
if (pHead1)
pLastNode->next = pHead1;
else if (pHead2)
pLastNode->next = pHead2;
return pRealHead;
}
};
int main () {
auto connect = [](auto& lhs, auto& rhs) {
lhs.next = &rhs;
};
ListNode n1(1);
ListNode n2(3);
ListNode n3(5);
ListNode n4(7);
ListNode n5(9);
ListNode n6(2);
ListNode n7(4);
ListNode n8(6);
connect(n1, n2);
connect(n2, n3);
connect(n3, n4);
connect(n4, n5);
connect(n6, n7);
connect(n7, n8);
Solution slu;
auto head = slu.mergeTwoLists(&n1, &n6);
while (head) {
std::cout << head->val << ", ";
head = head->next;
}
std::cout << std::endl;
return 0;
}
对3进行优化
3中代码, 使用变量pRealHead
来保存新链表的头节点,其次就是我们新链表由于是新构造的,所以总要在一个节点插入的时候判断是否该新链表构造了没有(pLastNdoe是否为空),因为一个节点插入空链表和非空链表逻辑是不一样的。所以为了统一和简化,我们假设插入的是一个非空链表,所以我们一开始要给出一个(假)头节点,这样就能保持所有逻辑都是插入非空链表的逻辑.
最终返回的链表就是这个假的头节点的下一个节点。代码如下:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode dummy(0);
ListNode* pLast = &dummy;
while (list1 && list2) {
if (list1->val <= list2->val) {
pLast->next = list1;
list1 = list1->next;
}
else {
pLast->next = list2;
list2 = list2->next;
}
pLast = pLast->next;
}
if (list1)
pLast->next = list1;
else
pLast->next = list2;
return dummy.next;
}
};