flowchart TD
A[开始] --> B{p1 是否为空}
B -->|No| C[保存 p1.next 到 temp]
C --> D[将 p1.next 指向 p2]
D --> E[更新 p2 为 p1]
E --> F[更新 p1 为 temp]
F --> B
B -->|Yes| G[返回 p2]
LeetCode:206.反转链表
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
let p1=head;
let p2=null
while(p1){
let temp=p1.next
p1.next=p2
p2=p1
p1=temp
}
return p2;
};
标签:p2,p1,temp,val,206,--,next,链表,LeetCode
From: https://www.cnblogs.com/KooTeam/p/18664165