24考研王道408数据结构-第二章“线性表”课后算法题(P40链表专题)

第一题:

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};
void deleteNode(listNode *&head,int x){
	if(head==NULL) return;
	listNode *P;
	if(head->val==x){
		P=head;
		head=head->next;
		free(P);
		deleteNode(head,x);
	}else{
		deleteNode(head->next,x);
	}
}

void disp(listNode *head){
	listNode *s=head;
	while(s){
		cout<<s->val<<"->";
		s=s->next;
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node5={5,nullptr};
	listNode node4={4,&node5};
	listNode node3={3,&node4};
	listNode node2={3,&node3};
	listNode node1={1,&node2};
	
	listNode *L=&node1;//指向头结点的指针
	disp(L);
	deleteNode(L,3);
	disp(L);
	return 0; 
}

在这里插入图片描述

第二题

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};
void deleteNode(listNode *&L,int x){
	listNode *p=L->next;
	listNode *pre=L;
	listNode *q;
	while(p!=NULL){
		if(p->val==x){
			q=p;
			p=p->next;
			pre->next=p;
			free(q);
		}else{
			pre=p;
			p=p->next;
		}
	}
}

void disp(listNode *head){
	listNode *s=head->next;
	while(s){
		cout<<s->val<<"->";
		s=s->next;
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node5={5,nullptr};
	listNode node4={4,&node5};
	listNode node3={3,&node4};
	listNode node2={3,&node3};
	listNode node1={1,&node2};
	listNode headNode={0,&node1};
	
	listNode *L=&headNode;//指向头结点的指针
	disp(L);
	deleteNode(L,3);
	disp(L);
	return 0; 
}

在这里插入图片描述
尾插法

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};
void deleteNode(listNode *&L,int x){
	listNode *p=L->next;
	listNode *r=L;
	listNode *pre;
	while(p!=NULL){
		if(p->val!=x){//当 p结点值不为x时将其链接到L的尾部 
			r->next=p;
			r=p;
			p=p->next;
		}
		else if(p->val==x){
			pre=p;
			p=p->next;
			free(pre);
		}
	}
	r->next=nullptr; 
}

void disp(listNode *head){
	listNode *s=head->next;
	while(s){
		cout<<s->val<<"->";
		s=s->next;
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node5={5,nullptr};
	listNode node4={4,&node5};
	listNode node3={3,&node4};
	listNode node2={3,&node3};
	listNode node1={1,&node2};
	listNode headNode={0,&node1};
	
	listNode *L=&headNode;//指向头结点的指针
	disp(L);
	deleteNode(L,3);
	disp(L);
	return 0; 
}

第三题:

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};

void redisp(listNode *head){
	if(head->next!=NULL){
		redisp(head->next);//递归,有栈保存前一个结点的值 
	}
	if(head!=NULL&&head->val!=NULL){
		cout<<head->val;
	}
}
void ignore_head(listNode* head){
	if(head->next!=NULL){
		redisp(head->next);
	}
}

int main(){
	listNode node5={5,nullptr};
	listNode node4={4,&node5};
	listNode node3={3,&node4};
	listNode node2={3,&node3};
	listNode node1={1,&node2};
	listNode headNode={NULL,&node1};
	
	listNode *L=&headNode;
	
	redisp(L);
}

在这里插入图片描述

第四题:

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};
void deleMin(listNode *&head){
	listNode *p=head->next,*pre=head, *minp=p,*minpre=pre;
	while(p!=NULL){
		if(p->val<minp->val){
			minp=p;
			minpre=pre;
		}
		pre=p;
		p=p->next;
	}
	minpre->next=minp->next;
	free(minp);
} 

void disp(listNode *head){
	listNode *s=head->next;
	while(s){
		cout<<s->val<<"->";
		s=s->next;
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node5={5,nullptr};
	listNode node4={2,&node5};
	listNode node3={3,&node4};
	listNode node2={1,&node3};
	listNode node1={4,&node2};
	listNode headNode={NULL,&node1};
	
	listNode *L=&headNode;
	disp(L);
	deleMin(L);
	disp(L);
}

在这里插入图片描述

第五题:

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};

listNode* reList(listNode* &head){
	listNode *curr=head->next;
	listNode *pre=head;
	listNode *p;
	head->next=NULL;
	while(curr!=NULL){
		p=curr->next;
		curr->next=pre;
		pre=curr;
		curr=p; 
	}
	listNode *newlist=pre;
	return newlist;
}

void disp(listNode *head){
	listNode *s=head;
	while(s!=NULL){
		cout<<s->val<<"->";
		s=s->next;
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node5={5,nullptr};
	listNode node4={2,&node5};
	listNode node3={3,&node4};
	listNode node2={1,&node3};
	listNode node1={4,&node2};
	listNode headNode={NULL,&node1};
	
	listNode *L=&headNode;
	disp(L);
	listNode *newL=reList(L);
	disp(newL);
}

在这里插入图片描述

第六题:

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};

void solution(listNode *&head){
	listNode *p=head->next; 
	listNode *pre;
	listNode *r=p->next;
	p->next=NULL;//只保留一个结点
	p=r;
	while(p!=NULL){//类似于直接插入排序 
		r=p->next;
		pre=head;
		while(pre->next!=NULL&&pre->next->val<p->val){
			pre=pre->next;//在有序表中从头开始遍历直到找到值小于结点p的结点 
		}
		p->next=pre->next;//将节点P插入有序表中 
		pre->next=p;
		p=r;
	}
}

void disp(listNode *head){
	listNode *s=head->next;
	while(s!=NULL){
		cout<<s->val<<"->";
		s=s->next;
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node5={5,nullptr};
	listNode node4={2,&node5};
	listNode node3={3,&node4};
	listNode node2={1,&node3};
	listNode node1={4,&node2};
	listNode headNode={NULL,&node1};
	
	listNode *L=&headNode;
	disp(L);
	solution(L); 
	disp(L);
}

在这里插入图片描述

第七题:

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};

void solution(listNode *&head,int minNum,int maxNum){
	listNode *p=head;
	listNode *pre;
	while(p->next!=NULL){
		if(p->next->val>=minNum && p->next->val<=maxNum){
			pre=p->next;
			p->next=p->next->next;
			free(pre);
		}else{
			p=p->next;
		}
	}
}

void disp(listNode *head){
	listNode *s=head->next;
	while(s!=NULL){
		cout<<s->val<<"->";
		s=s->next;
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node5={5,nullptr};
	listNode node4={2,&node5};
	listNode node3={3,&node4};
	listNode node2={1,&node3};
	listNode node1={4,&node2};
	listNode headNode={NULL,&node1};
	
	listNode *L=&headNode;
	disp(L);
	solution(L,2,4); 
	disp(L);
}

在这里插入图片描述

第八题:

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};
int Length(listNode *head){
	listNode *p=head->next;
	int num=0; 
	while(p!=NULL){
		p=p->next;
		num++;
	}
	return num;
} 

listNode* solution(listNode *&headA,listNode* &headB){
	
	int lenA=Length(headA);
	int lenB=Length(headB);
	listNode* shortlist;
	listNode* longlist;
	int dist=0;
	if(lenA>lenB){
		shortlist=headB;
		longlist=headA;
		dist=lenA-lenB;
	}else{
		shortlist=headA;
		longlist=headB;
		dist=lenB-lenA;
	}
	for(int i=0;i<dist;i++){
		shortlist=shortlist->next;
	}
	while(shortlist!=NULL&&longlist!=NULL){
		if(shortlist==longlist){
			return longlist;
		}else{
			shortlist=shortlist->next;
			longlist=longlist->next;
		}
	}
}

void disp(listNode *head){
	listNode *s=head;
	while(s!=NULL){
		cout<<s->val<<"->";
		s=s->next;
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node5={5,nullptr};
	listNode node4={2,&node5};
	listNode node3={3,&node4};
	listNode node2={1,&node3};
	listNode node1={4,&node2};
	listNode node0={9,&node1};
	listNode headNode={NULL,&node0};
	
	
	listNode nodeB2={8,&node2};
	listNode nodeB1={9,&nodeB2};
	listNode headNodeB={NULL,&nodeB1};
	
	listNode *LA=&headNode;
	listNode *LB=&headNodeB; 
	
	disp(LA);
	disp(LB);
	
	listNode *c;
	c=solution(LA,LB); 
	disp(c);
}

在这里插入图片描述

第九题:

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};
void solution(listNode* &head){
	listNode *p=head->next;
	listNode *r=p->next;
	listNode* pre;
	p->next=NULL;
	p=r;
	while(p!=NULL){//插入排序 
		pre=head;
		r=p->next;
		while(pre->next!=NULL&&pre->next->val<p->val){
			pre=pre->next;
		}
		p->next=pre->next;
		pre->next=p;
		p=r;
	}
}

void disp(listNode *head){
	listNode *s=head->next;
	listNode *temp;
	while(s!=NULL){
		temp=s;
		cout<<s->val<<"->";
		s=s->next;
		free(temp);
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node5={5,nullptr};
	listNode node4={2,&node5};
	listNode node3={3,&node4};
	listNode node2={1,&node3};
	listNode node1={4,&node2};
	listNode headNode={NULL,&node1};
	
	listNode *L=&headNode;
	disp(L);
	solution(L);
	disp(L);
}

在这里插入图片描述

第十题:

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};

listNode* reList(listNode* &head){
	listNode *newHeadB=new listNode;
	newHeadB->next=NULL;
	listNode *Bhead=newHeadB;
	listNode *p=head->next;
	listNode *pre=head;
	listNode *after;
	int num=1;
	while(p->next!=NULL){
		if(num%2!=0){
			pre=p;
			p=p->next;
			num++;
		}
		else if(num%2==0){
			after=p->next;
			pre->next=p->next;
			p->next=NULL;
			Bhead->next=p;
			Bhead=Bhead->next;
			p=after;
			num++;
		}
	}
	return newHeadB;
}

void disp(listNode *head){
	listNode *s=head->next;
	while(s!=NULL){
		cout<<s->val<<"->";
		s=s->next;
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node5={5,nullptr};
	listNode node4={4,&node5};
	listNode node3={2,&node4};
	listNode node2={3,&node3};
	listNode node1={1,&node2};
	listNode headNode={NULL,&node1};
	
	listNode *L=&headNode;
	disp(L);
	listNode *newL=reList(L);
	disp(L);
	disp(newL);
}

运行结果:
在这里插入图片描述

第十一题

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};

listNode* reList(listNode* &head){
	listNode *newHeadB=new listNode;
	newHeadB->next=NULL;
	listNode *Bhead=newHeadB;
	listNode *p=head->next;
	listNode *ra=head;
	listNode *temp;
	while(p!=NULL){
		ra->next=p;
		ra=p;
		p=p->next;
		if(p!=NULL){
			temp=p->next;
			p->next=Bhead->next;
			Bhead->next=p;
			p=temp;
		}
	}
	ra->next=NULL;
	return Bhead;
}

void disp(listNode *head){
	listNode *s=head->next;
	while(s!=NULL){
		cout<<s->val<<"->";
		s=s->next;
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node5={5,nullptr};
	listNode node4={4,&node5};
	listNode node3={3,&node4};
	listNode node2={2,&node3};
	listNode node1={1,&node2};
	listNode headNode={NULL,&node1};
	
	listNode *L=&headNode;
	disp(L);
	listNode *newL=reList(L);
	disp(L);
	disp(newL);
}

在这里插入图片描述

第十二题

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};

void solution(listNode *&head){
	listNode *p=head->next; 
	listNode *after;

	while(p->next!=NULL){
		after=p->next;
		if(p->val==after->val){
			p->next=after->next;
			free(after);
		}else{
			p=p->next;
		}
	}
}

void disp(listNode *head){
	listNode *s=head->next;
	while(s!=NULL){
		cout<<s->val<<"->";
		s=s->next;
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node5={30,nullptr};
	listNode node4={21,&node5};
	listNode node3={10,&node4};
	listNode node2={10,&node3};
	listNode node1={7,&node2};
	listNode headNode={NULL,&node1};
	
	listNode *L=&headNode;
	disp(L);
	solution(L); 
	disp(L);
}

在这里插入图片描述

第十三题

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};

listNode* solution(listNode *&la,listNode* &lb){
	listNode *pa=la->next;
	listNode *pb=lb->next;
	listNode *r;
	la->next=NULL;//la作为结果链表的头指针,先将结果链表初始化为空 
	while(pa&&pb){
		if(pa->val<=pb->val){
			r=pa->next;
			pa->next=la->next;
			la->next=pa;
			pa=r;
		}
		else{
			r=pb->next;
			pb->next=la->next;
			la->next=pb;
			pb=r; 
		}
	}
	if(pa)//如果是A剩余pb指向他,将剩余的链表指针统一用pb表示,减少重复代码量 
		pb=pa;
	while(pb){//采用头插法,将剩余链表插入,来保持递减次序 
		r=pb->next;
		pb->next=la->next;
		la->next=pb;
		pb=r;
		}
	free(lb); 
	return la;
 
}

void disp(listNode *head){
	listNode *s=head->next;
	while(s!=NULL){
		cout<<s->val<<"->";
		s=s->next;
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node6={9,nullptr};
	listNode node5={8,&node6};
	listNode node4={7,&node5};
	listNode node3={5,&node4};
	listNode node2={4,&node3};
	listNode node1={3,&node2};
	listNode node0={1,&node1};
	listNode headNode={NULL,&node0};
	
	
	listNode nodeB4={7,nullptr};
	listNode nodeB3={5,&nodeB4};
	listNode nodeB2={2,&nodeB3};
	listNode nodeB1={1,&nodeB2};
	listNode headNodeB={NULL,&nodeB1};
	
	listNode *LA=&headNode;
	listNode *LB=&headNodeB; 
	
	disp(LA);
	disp(LB);
	
	listNode *c;
	c=solution(LA,LB); 
	disp(c);
}

在这里插入图片描述

第十四题

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};

listNode* solution(listNode *&la,listNode* &lb){
	listNode *pa=la->next;
	listNode *pb=lb->next;
	listNode *lc=new listNode;
	lc->next=NULL;
	lc->val=0;
	listNode *pc=lc;
	while(pa!=NULL&&pb!=NULL){
		if(pa->val==pb->val){
			pc->next=pa;
			pc=pa;
			pa=pa->next;
			pb=pb->next;
		}
		else if(pa->val<pb->val){
			pa=pa->next;
		}else if(pb->val<pa->val){
			pb=pb->next;
		}
	}
	pc->next=NULL;//置结果链表尾指针为NULL 
	return lc;	 
}

void disp(listNode *head){
	listNode *s=head->next;
	while(s!=NULL){
		cout<<s->val<<"->";
		s=s->next;
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node6={9,nullptr};
	listNode node5={8,&node6};
	listNode node4={7,&node5};
	listNode node3={5,&node4};
	listNode node2={4,&node3};
	listNode node1={3,&node2};
	listNode node0={1,&node1};
	listNode headNode={NULL,&node0};
	
	
	listNode nodeB4={7,nullptr};
	listNode nodeB3={5,&nodeB4};
	listNode nodeB2={2,&nodeB3};
	listNode nodeB1={1,&nodeB2};
	listNode headNodeB={NULL,&nodeB1};
	
	listNode *LA=&headNode;
	listNode *LB=&headNodeB; 
	
	disp(LA);
	disp(LB);
	
	listNode *c;
	c=solution(LA,LB); 
	disp(c);
}

在这里插入图片描述

第十五题

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};

listNode* solution(listNode *&la,listNode* &lb){
	listNode *pa=la->next;
	listNode *pb=lb->next;
	listNode *pc=la;
	listNode *u;
	while(pa!=NULL&&pb!=NULL){
		if(pa->val==pb->val){
			pc->next=pa;
			pc=pa;
			pa=pa->next;
			u=pb;
			pb=pb->next;
			free(u);
		}
		else if(pa->val<pb->val){
			u=pa;
			pa=pa->next;
			free(u);
		}else if(pb->val<pa->val){
			u=pb;
			pb=pb->next;
			free(u);
		}
	}
	while(pa!=NULL){
		u=pa;
		pa=pa->next;
		free(u);
	}
	while(pb!=NULL){
		u=pb;
		pb=pb->next;
		free(u);//释放A中剩余的结点 
	}
	pc->next=NULL;//置结果链表尾指针为NULL 
	free(lb);
	return la;	 
}

void disp(listNode *head){
	listNode *s=head->next;
	while(s!=NULL){
		cout<<s->val<<"->";
		s=s->next;
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node6={9,nullptr};
	listNode node5={8,&node6};
	listNode node4={7,&node5};
	listNode node3={5,&node4};
	listNode node2={4,&node3};
	listNode node1={3,&node2};
	listNode node0={1,&node1};
	listNode headNode={NULL,&node0};
	
	
	listNode nodeB4={7,nullptr};
	listNode nodeB3={5,&nodeB4};
	listNode nodeB2={2,&nodeB3};
	listNode nodeB1={1,&nodeB2};
	listNode headNodeB={NULL,&nodeB1};
	
	listNode *LA=&headNode;
	listNode *LB=&headNodeB; 
	
	disp(LA);
	disp(LB);
	
	listNode *c;
	c=solution(LA,LB); 
	disp(c);
}

在这里插入图片描述

第十六题:

在这里插入图片描述

#include<iostream>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};

bool solution(listNode *&la,listNode* &lb){
	listNode *pa=la;
	listNode *pb=lb;
	listNode *pre=pa;
	
	while(pa!=NULL&&pb!=NULL){
		if(pb->val==pa->val){
			pa=pa->next;
			pb=pb->next;
		}
		else{
			pa=pa->next;
			pb=lb;//判断B是否是A的连续子序列,所以需要pb需要返回到B链表的第一个结点 
		}
	}
	if(pb==NULL){
		return true;
	}
	return false;
	
}

void disp(listNode *head){
	listNode *s=head;
	while(s!=NULL){
		cout<<s->val<<"->";
		s=s->next;
	}
	cout<<"NULL"<<endl;
}
int main(){
	listNode node6={9,nullptr};
	listNode node5={8,&node6};
	listNode node4={7,&node5};
	listNode node3={5,&node4};
	listNode node2={4,&node3};
	listNode node1={3,&node2};
	listNode node0={1,&node1};
	
	
	listNode nodeB4={7,nullptr};
	listNode nodeB3={5,&nodeB4};
	listNode nodeB2={4,&nodeB3};
	listNode nodeB1={3,&nodeB2};
	
	listNode *LA=&node0;
	listNode *LB=&nodeB1; 
	
	disp(LA);
	disp(LB);
	bool c;
	c=solution(LA,LB); 
	if(c){
		cout<<"true";
	}else{
		cout<<"false";
	}
}

在这里插入图片描述

第二十一题

在这里插入图片描述
方法一:哈希表判断有无环

#include<iostream>
#include<unordered_set>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};

bool reList(listNode* &head){
	listNode *p=head;
	unordered_set<listNode*> hashtable;
	while(p!=NULL){
		if(hashtable.count(p)){//插入之前先查哈希表看该节点之前是否出现过。 
			return true;
		}
		hashtable.insert(p);//如果表中未查到,则说明之前没有出现过,则插入哈希表中。	
		p=p->next;	
	}
	return false;
}

int main(){
	listNode node5={6,nullptr};
	listNode node4={4,&node5};
	listNode node3={2,&node4};
	listNode node2={3,&node3};
	listNode node1={1,&node2};
	node5={6,&node3};
	
	listNode *L=&node1;
	bool flag=reList(L);
	if(flag){
		cout<<"true";
	}
	else{
		cout<<"false"; 
	}
}

在这里插入图片描述
方法二:快慢指针判断有无环

#include<iostream>
#include<unordered_set>
using namespace std;
struct listNode{
	int val;
	struct listNode*next;
};

listNode* reList(listNode* &head){
	listNode *fast=head,*slow=head;
	while(fast!=NULL&&fast->next!=NULL){
		fast=fast->next->next;
		slow=slow->next;
		if(fast==slow){
			break;
		}
	}
	if(slow==NULL||fast->next==NULL){
		return NULL;//没有环,返回NULL 
	}
	listNode *p1=head, *p2=slow;
	while(p1!=p2){
		p1=p1->next;
		p2=p2->next;
	}
	return p1;//返回入口点 
}

int main(){
	listNode node5={6,nullptr};
	listNode node4={4,&node5};
	listNode node3={2,&node4};
	listNode node2={3,&node3};
	listNode node1={1,&node2};
	node5={6,&node3};
	
	listNode *L=&node1;
	listNode *point=reList(L);
	cout<<point->val; 
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值