zl程序教程

您现在的位置是:首页 >  后端

当前栏目

(链表)反转链表Reverse List

链表List 反转 reverse
2023-09-14 09:00:36 时间

逆转链表是简单而又简单的链表问题,其问题的方法之一可以设置三个指针,一个指向当前结点,一个指向前驱结点,一个指向后继指针

代码如下:

class Solution {
public:
   ListNode* ReverseList(ListNode* pHead) {
//      if(pHead==NULL || pHead->next==NULL)
//         return pHead;

        ListNode *cur=pHead;
        ListNode *pre=NULL;
        ListNode *tmp;

        while(cur){
            tmp=cur->next;
            cur->next=pre;
            pre=cur;
            cur=tmp;
        }

        return pre;
      }
};