leetcode linked link linked list flip

THE

problem

solution

Code


/*
struct ListNode {
   int val;
   struct ListNode *next;
   ListNode(int x) :
   		val(x), next(NULL) {
   }
};*/


/*
思路:   基本的链表操作,注意每次翻转要注意当前链表的地址要提前保存下来, 不然后面操作会让之前的丢失。

-   define pre , current 
-  while (head)
- -  temp = head->next 
- -  head->next = pre;
- -  pre = head;
--  head = temp;
- 
*/
 

class Solution {
    
    
public:
   ListNode* ReverseList(ListNode* pHead) {
    
    
       ListNode* pre=NULL, *temp = NULL;
       while(pHead){
    
    
           temp = pHead->next;
           pHead->next =pre;
           pre = pHead;
           pHead = temp;
       }
       return pre;
   }
};

Summary and reflection

  1. Note that before operating the linked list, be sure to see if your address will be overwritten. Do you want to find a temp and save it?

Guess you like

Origin blog.csdn.net/liupeng19970119/article/details/114236631