> For the complete documentation index, see [llms.txt](https://jenhsuan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jenhsuan.gitbook.io/algorithm/leetcode/206.-reverse-linked-list.md).

# 206. Reverse Linked List

## 1.問題

* 反轉linked list

![](/files/-LJaMCWxjTOgr_yXVVB9)

## 2.想法

* 提問
  * 確認題意:  反轉linked list
* function header, parameter
* test input
* 說明想法
  * 三個指標
    * current: 指向head, 下一回合時移到next
    * next: 指向head->next, 並讓current移動
    * pre: 一開始指向NULL, 並讓head->next移動
* 測試計算複雜度

## 3.程式碼

```
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if (head == NULL)
        {
            return NULL;
        }
        
        ListNode *prev = head;
        ListNode *curr = head->next;
        prev->next = NULL;
        
        while (curr != NULL)
        {
            ListNode *next = curr->next;
            curr->next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
};
```

## 4.Performance

![](/files/-LJaMS8S-cnHVNFydgrC)
