Shu-How Zの小窝

Loading...

LeetCode:206.反转链表

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;
};

image-20250110161249965

posted @ 2025-01-10 16:16  KooTeam  阅读(11)  评论(0)    收藏  举报