leetcode链表专题- 反转单链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head == nullptr)
        	return nullptr;
        if(head->next==nullptr)
        	return head;
        ListNode* s = head;
        ListNode* f = s->next;
        ListNode* help = f->next;
        s->next = nullptr;
        while(f != nullptr){
        	f->next = s;
        	if(help == nullptr)
        		return f;
        	s = f;
        	f = help;
        	help = help->next;
        }
        return nullptr;
    }
};

 

你可能感兴趣的:(leetcode链表专题)