链表合并中的析构问题(避免二次析构)

该博客讨论了在合并两个递增有序链表时如何处理析构问题,以防止二次析构导致的错误。在合并后,为避免在后续析构过程中对已析构节点的重复操作,应将原始链表头的next指针设为NULL。
摘要由CSDN通过智能技术生成


题目:

假定两个单链表是递增有序,定义并实现以下函数,完成两个单链表的合并,继续保持递增有序

int LL_merge(ListNode *La, ListNode *Lb)


#include<iostream>
using namespace std;
#define error 0
#define ok 1
class Node
{
public:
	int data;
	Node *next;
	Node() {next=NULL;}
};
class List
{
public:
	Node *head;
	int len;
	List()
	{
		head = new Node; 
		len=0;
	}
	~List()
	{
		Node *p,*q;
		p=head;
		while(p!=NULL)
		{
			q=p;
			p=p->next;
			delete []q;
		}
		len=0;
		head = NULL;
	}
	void InitList(int n)
	{
		int x,i;
		Node *tail;
		tail = head;
		for(i=0;i<n;i++)
		{
			cin>>x;
			Node
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值