题目描述
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
解题思路
/*** 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) {ListNode* pre_node = NULL;ListNode* curr = head;while (curr) {ListNode* temp = curr->next;curr->next = pre_node;pre_node = curr;curr = temp;}return pre_node;}};
