首页 > 代码库 > 206. Reverse Linked List(LeetCode)

206. Reverse Linked List(LeetCode)

Reverse a singly linked list.

/**
 * 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) {
    ListNode * pre=NULL;
        ListNode * current;
        while (head)
        {
            current = head;
            head = head->next;
            current->next=pre ;
            pre = current;
        }
        return pre;

    }
};

 

206. Reverse Linked List(LeetCode)