翻转链表
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* ReverseList(ListNode* pHead) { if (pHead == nullptr || pHead -> next == nullptr) return pHead; ListNode* currentNode = pHead; ListNode* preNode = nullptr; while (currentNode -> next != nullptr) { ListNode* nextNode = currentNode -> next; currentNode -> next = preNode; preNode = currentNode; currentNode = nextNode; } currentNode -> next = preNode; return currentNode; } };
posted on 2020-09-15 14:43 waterrzhang 阅读(102) 评论(0) 编辑 收藏 举报