c 语言实现链表反转(超详细,有手就行)

题目描述如下:

思路分析:

我们在对数组进行反转的时候,通常定义俩个指针,一个指向首元素,一个指向尾元素,然后借助一个中间变量,将俩者交换,然后首元素++;尾元素--;重复操作直到头元素大于等于尾元素。

但链表不同,它的空间是动态分配,即地址不一定是连续的。

那么如何实现呢?

头插

我的想法是用三个指针,先分别记录下前面三个的结点。即pre ,cur,next;

先将pre ->next=NULL;然后cur->next=pre ,这个时候我们实现了第一次头插,接下就是我们最喜欢的喜欢的过程了,将pre,cur,next这三个值更改,进行循环

即pre=cur,cur=next,next=next->next;

什么时候结束循环呢?那就是cur=NULL结束,但我们需要在对代码按上述操作的时候还需要一些小技巧!为什么?因为cur 为NULL时,在上一次循环中,next已经为NULL,我们接着操作next=next->next;是一个不合理的操作,即

在这里我们可以用一个判断来进行;如果next!=NULL;我们才next=next->next; 这样就可以异常(由于next=next->next   是为下一次循环做铺垫的,故对于最后一次没影响在里面)

画图分析是一个程序员不可忽视的一部分,我们下面通过图来描述下我的思路吧!

 

 

 这就是我描述的过程。

这也是基本情况之一,我们还应该注意,如果链表中无数据,或者只有一个数据是不需要进行操作!

以下是代码实现:

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 *
 * C语言声明定义全局变量请加上static,防止重复定义
 */

/**
 * 
 * @param pHead ListNode类 
 * @return ListNode类
 */

struct ListNode* ReverseList(struct ListNode* phead)
{
	if (phead == NULL)   //如果是空链表直接返回NULL
		return NULL;
	if (phead->next == NULL) //如果是只有一个结点的就把头返回就好了
		return phead;
	struct ListNode* pre = phead;
	struct ListNode* cur = phead->next;
	struct ListNode* next = cur->next;
	pre->next = NULL;//头插
	while (cur!=NULL)
	{
		cur->next = pre;
       pre=cur;
		cur = next;
		if(next!=NULL)//异常处理
		next = next->next;
	}
	return pre;

}

 想要练习这个题,可以通过牛客网,以下是链接:

谢谢你的观看,不要忘记点赞收藏等于学会,共同进步!https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=295&tqId=23286&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Fojxhttps://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=295&tqId=23286&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj

  • 13
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是双向链表反转的 C 语言代码: ```c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; struct Node* prev; }; void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } printf("\n"); } void reverseList(struct Node** head) { struct Node* temp = NULL; struct Node* current = *head; while (current != NULL) { temp = current->prev; current->prev = current->next; current->next = temp; current = current->prev; } if (temp != NULL) { *head = temp->prev; } } int main() { struct Node* head = NULL; struct Node* first = (struct Node*)malloc(sizeof(struct Node)); struct Node* second = (struct Node*)malloc(sizeof(struct Node)); struct Node* third = (struct Node*)malloc(sizeof(struct Node)); first->data = 1; first->prev = NULL; first->next = second; second->data = 2; second->prev = first; second->next = third; third->data = 3; third->prev = second; third->next = NULL; head = first; printf("Original List: "); printList(head); reverseList(&head); printf("Reversed List: "); printList(head); return 0; } ``` 在这个代码中,我们定义了一个 `Node` 结构体,包含了数据、前驱指针和后继指针。`printList` 函数用于打印链表中的每个元素。`reverseList` 函数用于反转链表,该函数的参数是一个指向指针的指针,因为我们需要在函数内部改变链表头指针的值。 在 `reverseList` 函数中,我们使用 `temp` 变量来保存当前结点的前驱指针,因为我们需要在交换当前结点的前驱和后继指针后,把它赋给下一个结点的前驱指针。最后,我们需要更新链表头指针的值,否则我们会得到一个不完整的链表。 在 `main` 函数中,我们创建了一个简单的双向链表,并将其传递给 `reverseList` 函数来反转它。最后,我们使用 `printList` 函数来检查链表是否被正确地反转。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个风轻云淡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值