[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
在做关于单链表的一些算法题的时候,往往需要将单链表逆置后操作更加方便,但是一般说起来逆置,常用循环遍历单链表,使用头插法再次创建
一个单链表实现逆置,但是这样不仅有点浪费存储空间,而且还容易搞混,那么如果要求空间复杂度为O(1)的话,那么就更不能使用头插法实现逆置了。
这时可以采用特定的算法实现单链表逆置,它的思想大概是这样的:
从头到尾扫描单链表,通过特定的手段使后面的一个结点依次指向前面的一个结点,这样,一个循环下去,那么这个单链表就实现了逆序操作。
即从
A---->A---->A----->A------>A----->A----->
变为
<-----A<-----A<-----A<-----A<-----A<-----A
可能似乎有点难以理解,我自己写了写草稿纸和代码,希望可以帮助大家理解:
因为最近在考研,时间紧,可能写的有点不规矩,嘿嘿,望谅解
C语言实现:
//将一个单链表 L 就地逆序存放 LinkList ReverseList(LinkList &L){ if(L->next == NULL)
//若单链表为空则直接返回 return L; LNode *q = L, *p = L->next, *temp;
//定义q指针指向前一个元素,p指针指向后一个元素,temp为临时指针 q->next = NULL; //将即将形成的链表链尾的指针域置 NULL while
(p != NULL){ q->data = p->data; //将后一个元素的值赋值给前一个元素 temp = p->next;
//temp暂时记录下一元素 p->next = q; //p指针此时反向指向q,即q将作为p的后继指针 q = p; //q指针后挪 p = temp;
//p指针后挪 } q->data = 0; //将最后形成的头结点数据域清 0 L = q; //最终 将 q赋值给 L 作为头结点 return L; }
运行截图: