方法思想:逆置链表初始为空,表中节点从原链表中依次“删除”,再逐个插入逆置链表的表头(即“头插”到逆置链表中),使它成为逆置链表的“新”的第一个结点,如此循环,直至原链表为空。
LNode *Inverse(LNode *L)
{
LNode *p, *q;
p = L->next;
L->next = NULL;
while (p != NULL)
{
q = p;
p = p->next;
q->next = L->next;
L->next = q;
}
return L;
}
接下来,进行图解:
刚开始是这样
’循环前的操作
进入循环,分别用q和p记录第一个和第二个节点
进入第二轮循环,这是发生重大变化的关键时期
![![![![![![![360截图172905071151341558.png](https://upload-images.jianshu.io/upload_images/20526589-055b0ae767956760.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
](https://upload-images.jianshu.io/upload_images/20526589-526e9856ea78b97e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
](https://upload-images.jianshu.io/upload_images/20526589-d41d68b939bd50cb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
](https://upload-images.jianshu.io/upload_images/20526589-6ee4d0677087b982.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
](https://upload-images.jianshu.io/upload_images/20526589-e26b2a5ee4cb112d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
](https://upload-images.jianshu.io/upload_images/20526589-b28f57162105008b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
](https://upload-images.jianshu.io/upload_images/20526589-df8da8ef173e1dcc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
直到链表为空