不加思索写出以下代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
struct ListNode* cur1=list1;
struct ListNode* cur2=list2;
if (list1==NULL)
return list2;
if (list2==NULL)
return list1;
struct newListNode
{
int new_val;
struct ListNode* new_next;
};
struct newListNode* new_next=NULL;
struct newListNode* newhead=NULL;
struct newListNode* m_m_new=(struct newListNode*)malloc(sizeof(struct newListNode));
newhead=m_m_new;
newhead->new_next=NULL;
struct newListNode* new_cur=newhead;
while(cur1!=NULL && cur2!=NULL)
{
if (cur1==NULL)
{
new_cur->new_val=cur2->val;
cur2=cur2->next;
//分配新结点的空间
struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));
new_cur->new_next=m_new;
new_cur=m_new;
continue;
}
if (cur2==NULL)
{
new_cur->new_val=cur1->val;
cur1=cur1->next;
struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));
new_cur->new_next=m_new;
new_cur=m_new;
continue;
}
if (cur1->val<=cur2->val)
{
new_cur->new_val=cur1->val;
cur1=cur1->next;
struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));
new_cur->new_next=m_new;
new_cur=m_new;
}
else
{
new_cur->new_val=cur2->val;
cur2=cur2->next;
//分配新结点的空间
struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));
new_cur->new_next=m_new;
new_cur=m_new;
}
}
new_cur->new_next=NULL;
new_cur=NULL;
return newhead;
}
运行时出现问题
发现while循环的条件写错了!!
应该改成
while(!(cur1==NULL && cur2==NULL))
完整代码
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
struct ListNode* cur1=list1;
struct ListNode* cur2=list2;
if (list1==NULL)
return list2;
if (list2==NULL)
return list1;
struct newListNode
{
int new_val;
struct ListNode* new_next;
};
struct newListNode* new_next=NULL;
struct newListNode* newhead=NULL;
struct newListNode* m_m_new=(struct newListNode*)malloc(sizeof(struct newListNode));
newhead=m_m_new;
newhead->new_next=NULL;
struct newListNode* new_cur=newhead;
struct newListNode* before_new_cur=NULL;
while(!(cur1==NULL && cur2==NULL))
{
if (cur1==NULL)
{
new_cur->new_val=cur2->val;
cur2=cur2->next;
struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));
new_cur->new_next=m_new;
before_new_cur=new_cur;
new_cur=m_new;
new_cur->new_next=NULL;
continue;
}
if (cur2==NULL)
{
new_cur->new_val=cur1->val;
cur1=cur1->next;
struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));
new_cur->new_next=m_new;
before_new_cur=new_cur;
new_cur=m_new;
continue;
}
if (cur1->val<=cur2->val)
{
new_cur->new_val=cur1->val;
cur1=cur1->next;
struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));
new_cur->new_next=m_new;
new_cur=m_new;
}
else
{
new_cur->new_val=cur2->val;
cur2=cur2->next;
struct newListNode* m_new=(struct newListNode*)malloc(sizeof(struct newListNode));
new_cur->new_next=m_new;
new_cur=m_new;
}
}
before_new_cur->new_next=NULL;
return newhead;
}
这里有一个潜在的问题:内存泄漏
应该在before_new_cur->new_next=NULL;下方加上free(new_next);
提交结果
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
struct ListNode* cur1=list1;
struct ListNode* cur2=list2;
struct ListNode* head=NULL;
struct ListNode* tail=NULL;
if (list1==NULL)
return list2;
if (list2==NULL)
return list1;
while (cur1 && cur2)
{
if (cur1->val<cur2->val)
{
if (head==NULL)
{
head=tail=cur1;
}
else
{
tail->next=cur1;
tail=tail->next;
}
cur1=cur1->next;
}
else
{
if (head==NULL)
{
head=tail=cur2;
}
else
{
tail->next=cur2;
tail=tail->next;
}
cur2=cur2->next;
}
}
if(cur1)
tail->next=cur1;
if(cur2)
tail->next=cur2;
return head;
}
尾插要有尾指针tail(这样不用频繁找尾),同时要有指向头节点的指针head用于返回
cur1->val<cur2->val和cur1->val>=cur2->val操作方式是类似的
当只有一个节点时,head==tail(即头==尾)
当cur1为NULL时,直接将剩余链表全部移动(head=tail=cur2;或head=tail=cur1;)
在方法1代码的基础上修改
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
struct ListNode* cur1=list1;
struct ListNode* cur2=list2;
struct ListNode* guard=NULL;
struct ListNode* tail=NULL;
guard=tail=(struct ListNode*)malloc(sizeof(struct ListNode));
tail->next=NULL;
if (list1==NULL)
return list2;
if (list2==NULL)
return list1;
while (cur1 && cur2)
{
if (cur1->val<cur2->val)
{
tail->next=cur1;
tail=tail->next;
cur1=cur1->next;
}
else
{
tail->next=cur2;
tail=tail->next;
cur2=cur2->next;
}
}
if(cur1)
tail->next=cur1;
if(cur2)
tail->next=cur2;
return guard->next;
}
虽然提交后能通过,但是有一个显然的问题:内存泄漏,要把头结点的空间释放掉
因此return guard->next;改为
struct ListNode* head=guard->next;
free(guard);
return head;
因篇幅问题不能全部显示,请点此查看更多更全内容