题目描述

输入一个链表,反转链表后,输出新链表的表头。
C++实现:
思路:头插法实现链表原地逆置
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        ListNode * linkList = new ListNode(0);
        
        ListNode * p = pHead;
        ListNode * nextP = p->next;
        
        while(p){
            p->next = linkList->next;
            linkList->next = p;
            p = nextP;
            nextP = nextP->next;
        }
        
        return linkList->next;
    }
};

剑指offer(十五):反转链表

 

 

相关文章:

  • 2022-12-23
  • 2021-06-03
  • 2021-08-13
  • 2021-09-12
  • 2021-05-04
  • 2021-09-04
  • 2022-01-10
猜你喜欢
  • 2021-12-18
  • 2021-12-18
  • 2021-11-06
  • 2021-10-25
  • 2021-08-19
  • 2022-01-10
  • 2022-12-23
相关资源
相似解决方案