LeetCode:反转链表

LeetCode:反转链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* reverseList(struct ListNode* head){

    if(head==NULL ||  head->next==NULL)
    return head;

    struct ListNode* pPre=head;
    struct ListNode* pCur=head->next;
    struct ListNode* pTmp=pCur;
    head->next=NULL;
    while(pTmp->next!=NULL)
    {
       pTmp=pTmp->next;
       pCur->next=pPre;
       pPre=pCur;
       pCur=pTmp;
    }
    pCur->next=pPre;
    return pCur;
}
posted @ 2021-09-28 12:35  无极至上  阅读(27)  评论(0编辑  收藏  举报