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;

    }
};

 

posted on 2017-05-10 08:56  无惧风云  阅读(117)  评论(0编辑  收藏  举报