写一个链表反转的c代码
时间: 2023-03-16 14:43:50 浏览: 57
struct Node *reverseList(struct Node *head){
struct Node *prev = NULL;
struct Node *curr = head;
struct Node *next = NULL;
while (curr != NULL) {
// Store the next node
next = curr->next;
// Reverse the current node's pointer
curr->next = prev;
// Move pointers to next node
prev = curr;
curr = next;
}
return prev;
}
相关问题
用c++写一段反转链表代码
### 回答1:
好的,以下是一段在 C 语言中实现链表反转的代码:
```
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *reverseList(struct node *head) {
struct node *prev = NULL;
struct node *current = head;
struct node *next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
return head;
}
int main() {
struct node *head = NULL;
// 此处省略链表的创建和初始化代码
head = reverseList(head);
// 此处省略遍历链表并打印元素的代码
return 0;
}
```
在这段代码中,我们定义了一个结构体 `node`,表示链表的节点,它包含两个成员:一个 `data` 成员,用于存储节点的数据;一个 `next` 指针,用于指向下一个节点。
反转链表的代码实现在 `reverseList` 函数中,它接收一个链表的头指针作为参数,并通过迭代实现对链表的反转。
在 `main` 函数中,我们创建了一个链表,并通过调用 `reverseList` 函数对其进行了反转。最后,我们可以遍历链表并打印元素。
### 回答2:
反转链表是将链表中的节点顺序倒过来的操作。下面是用C语言写的一段反转链表的代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 链表节点结构体
typedef struct Node {
int data;
struct Node* next;
} Node;
// 反转链表函数
Node* reverseList(Node* head) {
Node* prev = NULL;
Node* current = head;
Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
// 打印链表函数
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
Node* second = NULL;
Node* third = NULL;
// 创建链表节点
head = (Node*)malloc(sizeof(Node));
second = (Node*)malloc(sizeof(Node));
third = (Node*)malloc(sizeof(Node));
// 添加数据到节点中
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
printf("Original list: ");
printList(head);
head = reverseList(head);
printf("Reversed list: ");
printList(head);
return 0;
}
```
以上代码首先定义了链表节点的结构体,包含数据和指向下一个节点的指针。然后,定义了反转链表的函数`reverseList`,该函数使用三个指针prev、current和next来表示当前节点、前一个节点和下一个节点。通过迭代遍历链表,将当前节点的指针反向连接到前一个节点,直到遍历完整个链表。最后,定义了打印链表的函数`printList`,用于打印链表中节点的数据。在main函数中,创建了一个包含3个节点的链表,并分别赋值和连接。然后,使用`reverseList`函数将链表反转,并使用`printList`函数打印原链表和反转后的链表。
### 回答3:
反转链表是将链表中的节点顺序进行反转。我们可以使用C语言来实现这个功能。
假设我们已经有一个链表的头节点`head`,那么我们可以使用三个指针`prev`、`current`和`next`来进行反转操作。
首先,我们将`current`指针指向头节点`head`,将`prev`指针初始化为空指针,表示反转后的新链表的头节点为空。
然后,我们进入一个循环,只要`current`指针不为空,就进行如下操作:
1. 将`next`指针指向`current`的下一个节点,用于保存下一个节点的信息。
2. 将`current`节点的指针指向`prev`,完成当前节点的反转。
3. 将`prev`指针指向`current`,用于保存已经反转的部分链表。
4. 将`current`指针指向`next`,移动到下一个节点,继续进行反转。
最后,当循环结束时,`prev`指针就指向了反转后链表的头节点,我们可以将其作为返回值返回。
下面是具体的C语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构体
struct Node {
int data;
struct Node* next;
};
// 反转链表函数
struct Node* reverseList(struct Node* head) {
struct Node* prev = NULL;
struct Node* current = head;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
// 测试用例
int main() {
// 创建链表 1 -> 2 -> 3 -> 4 -> 5
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
head->data = 1;
struct Node* node1 = (struct Node*)malloc(sizeof(struct Node));
node1->data = 2;
head->next = node1;
struct Node* node2 = (struct Node*)malloc(sizeof(struct Node));
node2->data = 3;
node1->next = node2;
struct Node* node3 = (struct Node*)malloc(sizeof(struct Node));
node3->data = 4;
node2->next = node3;
struct Node* node4 = (struct Node*)malloc(sizeof(struct Node));
node4->data = 5;
node3->next = node4;
node4->next = NULL;
// 反转链表
struct Node* reversedHead = reverseList(head);
// 打印反转后的链表
printf("Reversed List: ");
struct Node* temp = reversedHead;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
return 0;
}
```
这段代码可以正确输出反转后的链表:
```plaintext
Reversed List: 5 4 3 2 1
```
这就是使用C语言编写的反转链表的代码。
头插法反转链表C语言代码
头插法是一种反转链表的方法,它通过将原链表的每个节点插入到新链表的头部,从而实现链表的反转。下面是使用头插法反转链表的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构体
struct ListNode {
int val;
struct ListNode* next;
};
// 头插法反转链表
struct ListNode* reverseList(struct ListNode* head) {
struct ListNode* newHead = NULL; // 新链表的头节点
while (head != NULL) {
struct ListNode* nextNode = head->next; // 保存下一个节点的指针
head->next = newHead; // 将当前节点插入到新链表的头部
newHead = head; // 更新新链表的头节点
head = nextNode; // 移动到下一个节点
}
return newHead;
}
// 创建链表
struct ListNode* createList(int* nums, int size) {
struct ListNode* head = NULL;
struct ListNode* tail = NULL;
for (int i = 0; i < size; i++) {
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->val = nums[i];
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
return head;
}
// 打印链表
void printList(struct ListNode* head) {
while (head != NULL) {
printf("%d ", head->val);
head = head->next;
}
printf("\n");
}
int main() {
int nums[] = {1, 2, 3, 4, 5};
int size = sizeof(nums) / sizeof(nums[0]);
struct ListNode* head = createList(nums, size);
printf("原链表:");
printList(head);
struct ListNode* newHead = reverseList(head);
printf("反转后的链表:");
printList(newHead);
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)