troubleasy

导航

 

输入一个链表,反转链表后,输出新链表的表头。

思路:pre指向第一个节点,current指向下个节点,while current不是空,保存current的下个节点,令current->next=pre;pre=current;current=current->next;

    ListNode* ReverseList(ListNode* pHead) {
        ListNode *pre,*nextNode,*current;
        if(pHead==NULL||pHead->next==NULL) return pHead;
        pre=pHead;
        current=pHead->next;
        pHead->next=NULL;
        while(current!=NULL)
        {
            nextNode=current->next;
            current->next=pre;
            pre=current;
            current=nextNode;
        }
        //current->next=pre;
        //Rhead=current;
        return pre;
    }

posted on 2020-05-26 01:45  troubleasy  阅读(87)  评论(0编辑  收藏  举报