LeetCode Easy206 初级算法 反转链表 C语言实现以及本地测试

该博客介绍了如何使用C语言和栈数据结构实现LeetCode中的206题——反转链表。首先定义了链表节点结构和栈结构,接着实现了Push、Pop和isEmpty等栈操作函数。然后通过创建链表、反转链表和输出链表值的函数,展示了反转链表的具体过程。在main函数中进行了测试,验证了反转链表的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LeetCode Easy206 初级算法 反转链表 C语言实现以及本地测试

题目描述:

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     struct ListNode *next;
* };
*/
struct ListNode *reverseList(struct ListNode *head)
{

}

由于我打算使用栈来完成反转,所以要先定义栈。

//创建栈数据结构
typedef struct NodeStack
{
    struct ListNode *Data[5000];//输入的链表长度最大为5000
    int topIdx;
} NodeStack;

还需要一些关于栈的基本函数,如Push(), Pop(), isEmpty()

//Push(),将一个结点e放入栈顶
struct ListNode *Push(NodeStack *L, struct ListNode *e)
{
    if (L->topIdx == 5000 - 1)
    {
        return 0;
    }
    L->Data[++L->topIdx] = e;
    return e;
}
//Pop(),弹出栈顶元素
struct ListNode * Pop(NodeStack *L)
{
	//栈为空则返回空指针
    if (L->topIdx == -1)
    {
        return NULL;
    }
    //返回栈顶结点
    return L->Data[L->topIdx--];
}
//isEmpty(),用来判断栈是否为空
bool isEmpty(NodeStack *L)
{
	//栈顶坐标为-1则为空
    if (L->topIdx == -1)
    {
        return true;
    }
    return false;
}

由于是本地测试,我又写了用来创建链表的newListNode()和打印输出链表的showListNodeVal()

//通过输入数组来创建链表,返回链表的头指针
struct ListNode *newListNode(int *arr, int arrSize)
{

	//创建头结点
    struct ListNode *head = (struct ListNode *)malloc(sizeof(struct ListNode)); 
    
    head->val = *(arr++);
    head->next = NULL;
    struct ListNode *tmp = head;
    for (int i = 1; i < arrSize; i++)
    {
        struct ListNode *a = (struct ListNode *)malloc(sizeof(struct ListNode));
        a->val = *(arr++);
        a->next = NULL;
        tmp->next = a;
        tmp = tmp->next;
    }
    return head;
}
void showListNodeVal(struct ListNode *head)
{
    if (head == NULL)
    {
        printf("[]");
        return;
    }
    printf("[");
    while (head != NULL)
    {
        if (head->next == NULL)
        {
            printf("%d", head->val);
        }
        else {
            printf("%d, ", head->val);
        }
        head = head->next;
    }
    printf("]\n");
}

接下来就是写reverseList()了。

//反转链表
struct ListNode *reverseList(struct ListNode *head)
{
    /*
     *使用栈
    */
    NodeStack nodeStack;//创建栈
    nodeStack.topIdx = -1;//栈内无元素
    //先把所有的结点放入栈内
    while (head != NULL)
    {
        Push(&nodeStack, head);
        head = head->next;
    }
    //若栈内无元素则返回空指针
    if (isEmpty(&nodeStack))
    {
        return NULL;
    }
    struct ListNode *node = Pop(&nodeStack);//取出第一个栈顶指针
    struct ListNode *newHead = node;//记录它为头指针
    //去除所有的栈内元素
    while (!isEmpty(&nodeStack))
    {
        struct ListNode *tmpNode = Pop(&nodeStack);
        node->next = tmpNode;
        node = node->next;
    }
    node->next = NULL;
    return newHead;
}

接下来就是在main()里面测试了!

int main()
{
    //设置测试数据
    int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    //创建链表
    struct ListNode *listNode = newListNode(arr, 10);
    //输出当前链表值
    showListNodeVal(listNode);
    //反转链表
    listNode = reverseList(listNode);
    //输出当前链表值
    showListNodeVal(listNode);
}

以下为输出结果:

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

在LeetCode里提交的代码如下:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

typedef struct NodeStack
{
    struct ListNode *Data[5000];
    int topIdx;
} NodeStack;

struct ListNode *Push(NodeStack *L, struct ListNode *e)
{
    if (L->topIdx == 5000 - 1)
    {
        return NULL;
    }
    L->Data[++L->topIdx] = e;
    return e;
}

struct ListNode *Pop(NodeStack *L)
{
    if (L->topIdx == -1)
    {
        return NULL;
    }
    return L->Data[L->topIdx--];
}

bool isEmpty(NodeStack *L)
{
    if (L->topIdx == -1)
    {
        return true;
    }
    return false;
}

struct ListNode* reverseList(struct ListNode* head){
    /*
     *使用栈
    */
    NodeStack nodeStack;
    nodeStack.topIdx = -1;
    while (head != NULL)
    {
        Push(&nodeStack, head);
        head = head->next;
    }
    if (isEmpty(&nodeStack))
    {
        return NULL;
    }
    struct ListNode *node = Pop(&nodeStack);
    struct ListNode *newHead = node;
    while (!isEmpty(&nodeStack))
    {
        struct ListNode *tmpNode = Pop(&nodeStack);
        node->next = tmpNode;
        node = node->next;
    }
    node->next = NULL;
    return newHead;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值