leetcode Reverse Linked List 单链表反转版本二

没想到这么快就1A版本二,链表实现的操作。
/**
 * 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 == NULL)
            return NULL;
        ListNode * p;
        ListNode * q;
        ListNode * now;
        p = head;
        now = head;
        q = p -> next;
        while(q != NULL)
        {
            p -> next = q -> next;
            q -> next = now;
            now = q;
            q = p-> next;
        }
        return now;
    }
};

你可能感兴趣的:(LeetCode,链表,数组,单链表)