//反转链表 -- 迭代
/*
只需要完成逆置链表函数
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode *prev = nullptr;
ListNode *cur = pHead;
while (cur)
{
ListNode *next = cur->next;
cur->next = prev;
prev = cur, cur = next;
}
return prev;
}
};
/*
只需要完成逆置链表函数
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode *prev = nullptr;
ListNode *cur = pHead;
while (cur)
{
ListNode *next = cur->next;
cur->next = prev;
prev = cur, cur = next;
}
return prev;
}
};
2020-05-06
在牛客打卡14天,今天学习:刷题 3 道/代码提交 3 次
全部评论
相关推荐
11-02 23:41
内蒙古工业大学 Java 点赞 评论 收藏
分享
10-10 16:56
武汉工程大学 嵌入式工程师 点赞 评论 收藏
分享
阿里云工作强度 694人发布
