题目要求
![在这里插入图片描述](https://img-blog.csdnimg.cn/4ee7b6c8a40c4f6a8d2da1efd75337a8.png)
代码实现
思路一
struct ListNode
{
int val;
struct ListNode *next;
};
struct ListNode* reverseList(struct ListNode* head)
{
if (head == NULL || head->next == NULL)
{
return head;
}
struct ListNode *prev, *cur, *next;
prev = NULL;
cur = head;
next = head->next;
while (cur)
{
cur->next = prev;
prev = cur;
cur = next;
if (next)
{
next = next->next;
}
}
return prev;
}
思路二
struct ListNode
{
int val;
struct ListNode *next;
};
struct ListNode* reverseList(struct ListNode* head)
{
struct ListNode* newhead = NULL;
struct ListNode* cur = head;
while (cur)
{
struct ListNode* next = cur->next;
cur->next = newhead;
newhead = cur;
cur = next;
}
return newhead;
}