LeetCode206——Reverse Linked List 反转链表


Reverse a singly linked list.


实现:

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if (head == NULL) return NULL;
        ListNode* pfirst = head;
        ListNode* pcurr = head;
        while (pcurr->next) {
            ListNode* tmp = pcurr->next;
            pcurr->next = tmp->next;
            tmp->next = pfirst;
            pfirst = tmp;
        }
        return pfirst;
    }
};

你可能感兴趣的:(面试题算法题)