反转链表
题目:输入一个链表,反转链表后,输出新链表的表头。
解题思路:
此题应该也是第三次解,综上第三次解思路可以打开,需要设定什么未知数,返回值类型等。。
1:nex保存当前操作结点的后续部分,防止丢失。
2:cur当前操作,将其与pre链接,连在pre之后。
3:更新pre,将当前结点的值给pre,即下一结点操作时也是插入pre之前。
4:更新cur值,跳至下一个需要连接的结点。
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode *cur = pHead;
ListNode *nex = nullptr;
ListNode *pre = nullptr;
while(cur){
nex = cur->next;
cur->next = pre;
pre = cur;
cur = nex;
}
return pre;
}
};