
链表的构建带头节点的构建typedefstructLNode{intdata;structLNode*next;}LNode;LNode*create(intn){LNode*head(LNode*)malloc(sizeof(LNode));head-nextNULL;LNode*tailhead;for(inti0;in;i){LNode*s(LNode*)malloc(sizeof(LNode));intd0;scanf(%d,d);s-datad;s-nextNULL;tail-nexts;tails;}returnhead;}不带头节点typedefstructLNode{intdata;structLNode*next;}LNode;LNode*create(intn){LNode*headNULL,*tailNULL;for(inti0;in;i){intb0;LNode*s(LNode*)malloc(sizeof(LNode));scanf(%d,b);s-datab;s-nextNULL;if(headNULL){headtails;}else{tail-nexts;tails;}}returnhead;}将链表倒过来LNode*reverse(LNode*head){LNode*preNULL;while(head!NULL){LNode*nexthead-next;head-nextpre;prehead;headnext;}headpre;returnhead;}新建一个临时节点next用来过渡给以理解为将一个链条分为两条在拼接。求倒数第几个还有在单项链表求它倒数第几个可以用指针来求LNode*findK(LNode*head,intk){if(k0){returnNULL;}LNode*fasthead-next;LNode*slowhead-next;for(inti0;ik;i){if(fastNULL){returnNULL;}fastfast-next;}while(fast!NULL){fastfast-next;slowslow-next;}returnslow;}求环入口用一个快指针和慢指针来求先找到第一次相遇找到节点再将slow重新赋值为head在找相遇就是环入口节点。LNode*entry(LNode*head){LNode*slowhead;LNode*fasthead;while(fast!NULLfast-next!NULL){slowslow-next;fastfast-next-next;if(slowfast){break;}}if(fastNULL||fast-nextNULL){returnNULL;}slowhead;while(slow!fast){slowslow-next;fastfast-next;}returnslow;}总结快慢指针多作用有很多还有链表倒叙用法挺广的