C++ 反转单向链表
生活随笔
收集整理的這篇文章主要介紹了
C++ 反转单向链表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
struct ListNode {int val;struct ListNode *next; ListNode(int x) :val(x), next(NULL) {}
};
//核心思路:將前后兩個節點的指向關系交換從->變成<-
class Solution {
public:ListNode* ReverseList(ListNode* pHead) {ListNode* pre = nullptr;ListNode* cur = pHead;ListNode* next = nullptr;while(cur){next = cur->next;cur->next = pre;pre = cur;cur = next;}return pre;}
};
總結
以上是生活随笔為你收集整理的C++ 反转单向链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 逆向去除winrar广告
- 下一篇: leetcode 小青蛙跳梯子