Simple C++ Solution


  • 0
    C

    class Solution {
    public:
    ListNode* reverseList(ListNode* head) {
    if(head==NULL) return NULL;

        ListNode* prev = NULL;
        ListNode* cur= head;
        ListNode* next = head->next;
        
        while(cur)
        {
            next = cur->next;
            cur->next = prev;
            prev= cur;
            cur = next;
        }
        return prev;
        
    }
    

    };


  • 0
    H

    class Solution {
    public:
    ListNode* reverseList(ListNode* head) {
    if(head==NULL || head->next==NULL) {
    return head;
    }
    ListNode* pre=head;
    ListNode* p=head->next;
    head->next=NULL;
    while(p!=NULL) {
    ListNode* tmp = p->next;
    p->next = pre;
    pre=p;
    p=tmp;
    }
    return pre;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.