第一题:
#include
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
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
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){
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
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
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
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
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->next=pre->next;
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
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
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
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
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
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
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
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;
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)
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
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;
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
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);
}
pc->next=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
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;
}
}
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
#include
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
#include
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;
}
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;
}