LeetCode 206. Reverse Linked List 时间复杂度(O( n))

 时间复杂度(O( n))

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

 

你可能感兴趣的:(LeetCode)