0 概述
链表作为一种基础的数据结构,在很多地方会用到。如在Linux内核代码,redis源码,python源码中都有使用。除了单向链表,还有双向链表,本文主要关注单向链表(含部分循环链表题目,会在题目中注明,其他情况都是讨论简单的单向链表)。双向链表在redis中有很好的实现,也在我的仓库中拷贝了一份用于测试用,本文的相关代码在 这里。
https://github.com/shishujuan/data-structure-algorithms
1 定义
先定义一个单向链表结构,如下,定义了链表结点和链表两个结构体。这里我没有多定义一个链表的结构体,保存头指针,尾指针,链表长度等信息,目的也是为了多练习下指针的操作。
// aslist.h
// 链表结点定义
typedef struct ListNode {
struct ListNode *next;
int value;
} listNode;
2 基本操作
在上一节的链表定义基础上,我们完成几个基本操作函数,包括链表初始化,链表中添加结点,链表中删除结点等。
/**
* 创建链表结点
*/
ListNode *listNewNode(int value)
{
ListNode *node;
if (!(node = malloc(sizeof(ListNode))))
return NULL;
node->value = value;
node->next = NULL;
return node;
}
/**
* 头插法插入结点。
*/
ListNode *listAddNodeHead(ListNode *head, int value)
{
ListNode *node;
if (!(node = listNewNode(value)))
return NULL;
if (head)
node->next = head;
head = node;
return head;
}
/**
* 尾插法插入值为value的结点。
*/
ListNode *listAddNodeTail(ListNode *head, int value)
{
ListNode *node;
if (!(node = listNewNode(value)))
return NULL;
return listAddNodeTailWithNode(head, node);
}
/**
* 尾插法插入结点。
*/
ListNode *listAddNodeTailWithNode(ListNode *head, ListNode *node)
{
if (!head) {
head = node;
} else {
ListNode *current = head;
while (current->next) {
current = current->next;
}
current->next = node;
}
return head;
}
/**
* 从链表删除值为value的结点。
*/
ListNode *listDelNode(ListNode *head, int value)
{
ListNode *current=head, *prev=NULL;
while (current) {
if (current->value == value) {
if (current == head)
head = head->next;
if (prev)
prev->next = current->next;
free(current);
break;
}
prev = current;
current = current->next;
}
return head;
}
/**
* 链表遍历。
*/
void listTraverse(ListNode *head)
{
ListNode *current = head;
while (current) {
printf("%d", current->value);
printf("->");
current = current->next;
if (current == head) // 处理首尾循环链表情况
break;
}
printf("NULL\n");
}
/**
* 使用数组初始化一个链表,共len个元素。
*/
ListNode *listCreate(int a[], int len)
{
ListNode *head = NULL;
int i;
for (i = 0; i < len; i++) {
if (!(head = listAddNodeTail(head, a[i])))
return NULL;
}
return head;
}
/**
* 链表长度函数
*/
int listLength(ListNode *head)
{
int len = 0;
while (head) {
len++;
head = head->next;
}
return len;
}
3 链表相关面试题
3.1 链表逆序
题: 给定一个单向链表 1->2->3->NULL,逆序后变成 3->2->1->NULL。
解:常见的是用的循环方式对各个结点逆序连接,如下:
/**
* 链表逆序,非递归实现。
*/
ListNode *li