问题描述:
单链表反转就是把原单向链表顺序倒置。虽然这仅仅是链表操作中比较基础的一个,但是熟练写出单链表反转,对很多刚刚接触代码不足一年的同学来说仍然有一定难度。
这篇博客将使用 C语言简单实现这一操作,并与完整的输入输出功能一起实现,方便读者运行代码进行测试。
代码如下:
/**
* (C) Copyright Bob Ding, Xidian University. 2021.
*/
#include <stdio.h>
#include <stdlib.h>
/**
* @brief inits the type of node.
* @brief creates data to instore data.
* @brief creates next to instore the address of other node.
*/
typedef struct Node
{
int data;
struct Node *next;
}node;
node *init_list();
node *reverse_list(node *head);
void display_list(node *head);
void free_list(node *head);
int main()
{
node *head = init_list();
reverse_list(head);
display_list(head);
free_list(head);
return 0;
}
/**
* @brief inits the linklist.
* @brief creates head node without data.
* @brief creates cur node in order to traverse the list.
* @brief inserts N numbers into the list.
* @note notices that head is a node without data.
* @param
* @return head – the first node of this list
*/
node *init_list()
{
node *head = (node *)malloc(sizeof(node));
node *cur = head;
int N;
printf("请先输入你要输入的数字个数:\n");
scanf("%d", &N);
if (N <= 0)
return NULL;
for (int i = 0; i < N; i++)
{
node *a = (node *)malloc(sizeof(node));
scanf("%d", &(a -> data));
a -> next = NULL;
cur -> next = a;
cur = cur -> next;
}
return head;
}
/**
* @brief displays this list.
* @param head – the first node of this list
* @return
*/
void display_list(node *head)
{
node *cur = (node *)malloc(sizeof(node));
cur = head;
while ((cur = cur -> next))
{
printf("%d ", cur -> data);
}
free(cur);
printf("\n");
}
/**
* @brief reverses this list.
* @note notices that head is a node without data.
* @param head – the first node of the list
* @return pre – the new first node of the reversed list
*/
node *reverse_list(node *head)
{
node *cur = NULL;
node *next = NULL;
cur = head -> next;
next = head -> next -> next;
while (next != NULL)
{
cur -> next = next -> next;
/* Removes the next node from this list. */
next -> next = head -> next;
/* Moves the next node before the cur node in this list. */
head -> next = next;
next = cur -> next;
}
return head;
}
/**
* @brief creates free_list function to free all space of the list.
* @note head is a node without data.
* @param head – the first node of this list
* @return
*/
void free_list(node *head)
{
if (head -> next == NULL)
return;
node *cur = head;
while ((cur = cur -> next))
{
head -> next = cur -> next;
free(cur);
cur = head;
}
free(head);
}
思路
单链表反转的代码在reverse_list()函数里,主要利用的指针操作。这个函数先传入一个自己定义的链表节点类型的指针变量head,这是一个存储了链表第一个节点地址的变量。请读者仔细阅读init_list()函数的实现。实际上这个链表设计了一个空的头节点,也就是说,链表的第一个节点里虽然存有下一个节点的地址,但是不存数据。
然后我在reverse_list()函数里定义了cur "current"还有next 两个节点类型的指针变量。用cur去遍历链表的各个节点,用next存储每个cur节点连接的下一个节点的地址。
要想反转,就是要把前一个节点变成下一个节点,把下一个节点变成前一个节点。因为头节点数据为空,所以数据实际上是从第二个节点开始存储。我先让第二个节点指向第三个节点的指针指到第四个节点。这样就把第三个节点从链表中移出来了。我们接着把原来第三个节点指向下一个节点的指针指到第二个节点。最后把头节点指向原第二个节点的指针指到原第三个节点,这样就完成了把第三个节点移到第二个节点前到操作,一直移动到最后一个节点就能完成反转。
h e a d → 2 → 3 → 4 → . . . → t a i l head \rightarrow 2\rightarrow3 \rightarrow4 \rightarrow... \rightarrow tail head→2→3→4→...→tail
第一次移动节点
h e a d → 3 → 2 → 4 → . . . → t a i l head \rightarrow 3\rightarrow2 \rightarrow4\rightarrow... \rightarrow tail head→3→2→4→...→tail
第二次移动节点
h e a d → 4 → 3 → 2 → . . . → t a i l head \rightarrow 4\rightarrow3 \rightarrow2\rightarrow... \rightarrow tail head→4→3→2→...→tail
直至遍历到最后一个节点
h e a d → t a i l → . . . → 4 → 3 → 2 head \rightarrow tail \rightarrow... \rightarrow 4\rightarrow 3\rightarrow 2 head→tail→...→4→3→2
这样便顺利完成了反转