反转链表 循环倒着建链表 - lifengyu360/lifengyu_first_git_test GitHub Wiki
/**
- Definition for singly-linked list.
- struct ListNode {
-
int val; -
ListNode *next; -
ListNode() : val(0), next(nullptr) {} -
ListNode(int x) : val(x), next(nullptr) {} -
ListNode(int x, ListNode *next) : val(x), next(next) {} - }; */
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (head == nullptr){
return head;
}
ListNode *new_head = head;
ListNode *tail = head->next;
new_head->next = nullptr;
while (tail){
ListNode *tmp = tail->next;
tail->next = new_head;
new_head = tail;
tail = tmp;
}
return new_head;
}
};