反转整个链表

  1. 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    struct ListNode* ReverseList(struct ListNode* pHead ) {
    // write code here
    if(pHead==NULL){
    return NULL;
    }
    struct ListNode* pre=NULL;
    struct ListNode* cur=pHead;
    while(cur!=NULL){
    struct ListNode* temp=cur->next;
    cur->next=pre;
    pre=cur;
    cur=temp;
    }
    return pre;
    }
  2. 理解

    • temp保存cur->next的地址
    • pre第一次要为NULL

反转整个链表
https://tsy244.github.io/2023/04/10/算法/数据结构/反转整个链表/
Author
August Rosenberg
Posted on
April 10, 2023
Licensed under