//反转链表 -- 迭代
/*
只需要完成逆置链表函数
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 次
全部评论
相关推荐
点赞 评论 收藏
分享