206. Reverse Linked List

# Easy

Key idea: record pre-node + current node + change next direction

process of reverse a linked list

Solution

temp records next node, head is current node, preNode is the pre-node of head. Don't forget to initialise preNode = NULL, return new head.

temp 记录下一个节点,head表示当前节点,preNode表示head的母节点。必须初始化preNode=NULL,并且最后返回新的头节点(即最后一个节点)。

  1. record next node temporally

  2. let head.next point to preNode

  3. move to next node

  4. 暂时记录下一个节点

  5. 让head.next指向前一个节点

  6. 指针后移一位

Python syntax

Last updated

Was this helpful?