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;
}