剑指offer[15]——反转链表
发布日期:2021-05-13 01:00:29 浏览次数:12 分类:博客文章

本文共 1467 字,大约阅读时间需要 4 分钟。

������������

������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������

������������

������������������������������������������������������������������������������������������������������������������������������������������������������

/*function ListNode(x){    this.val = x;    this.next = null;}*/function ReverseList(pHead){    let temp = [];    while(pHead){        temp.unshift(pHead.val);        pHead = pHead.next;    }    if(temp.length == 0){return pHead;}    let head = new ListNode(temp[0]);    let res = head;    for(let i=1; i

������

���������������������������������������������������������������������������������������������

������������������������������������������������������������������������������������������pre���cur���������������������cur���������������������������������������cur.next���������������������������������������������cur.next���������������������������������������������������������������������������cur.next=pre���������������������������������������������������������������������

/*function ListNode(x){    this.val = x;    this.next = null;}*/function ReverseList(pHead){    if(!pHead || !pHead.next){return pHead;}    let cur = pHead.next;    let pre = pHead;    pre.next = null;    while(cur){        let temp = cur.next;        cur.next = pre;        pre = cur;        cur = temp;    }    return pre;}
上一篇:剑指offer[16]——合并两个排序的链表
下一篇:剑指offer[14]——链表中倒数第k个结点

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月28日 16时14分40秒