--- title: 'LeetCode 206. Reverse Linked List' disqus: hackmd --- # LeetCode 206. Reverse Linked List ## Description Given the head of a singly linked list, reverse the list, and return the reversed list.![](https://i.imgur.com/6GGbtPZ.png) ## Example Input: head = [1,2,3,4,5] Output: [5,4,3,2,1] ## Constraints The number of nodes in the list is the range [0, 5000]. -5000 <= Node.val <= 5000 ## Answer 此題可設2個點,第一是原list,第二是下一點。 先暫存下一點,然後將原list點接上反轉點,最後將反轉點更新為原list點,原list點更新為暫存點。 ```Cin= //2021_03_12 struct ListNode* reverseList(struct ListNode* head){ struct ListNode *cur = NULL, *nxt = NULL; while(head!=NULL){ nxt = head->next; head->next = cur; cur = head; head = nxt; } return cur; } ``` ## Link https://leetcode.com/problems/reverse-linked-list/ ###### tags: `Leetcode`