题解 | #反转链表#
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
1 基础功能
非递归的反转, 从尾巴 next curr->next; curr pre 关系的整合
2 代码
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode *pre = nullptr, *curr=pHead,*next;
while(curr){ //三元组的判定
next = curr->next;
curr->next = pre ;
pre = curr;
curr = next;
}
return pre;//not curr; //三元组的默契
}
};