My C++ solution


  • 0
    class Solution {
    public:
        ListNode* reverseList(ListNode* head) {
            if (head == nullptr || head->next == nullptr)	return head;
    		ListNode* pos = head->next;
    		head->next = nullptr;
    		ListNode* temp = nullptr;
    		while (pos){
    			temp = pos->next;
    			pos->next = head;
    			head = pos;
    			pos = temp;
    		}
    		return head;
        }
    };

Log in to reply
 

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