lc19-删除链表中的倒数第n个结点

#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct node{
    int data;
    node *next;
}linklist;
linklist *deleten(linklist *head,int n){
    linklist *head1,*p,*q;
    head1=head;
    q=head;
    p=head;
    while(n--!=0){
    
        p=p->next;
    }
    if(head1==NULL){
        return head;
    }
    while(p->next!=NULL){
        p=p->next;
        q=q->next;
    }
    linklist *k;
    k=q->next;
    q->next=k->next;
    free(k);
    return head;
}
void main(){
    int n;
    scanf("%d",&n);
    linklist *head,*k;
    head=(linklist*)malloc(sizeof(linklist));
    head->next=NULL;
    k=head;
    for(int i=0;i<n;i++){
        linklist *p;
        p=(linklist*)malloc(sizeof(linklist));
        scanf("%d",&p->data);
        p->next=k->next;
        k->next=p;
        k=p;

    }
    head=deleten(head,3);
    while(head->next!=NULL){
        printf("%d",head->next->data);
        head=head->next;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容