Reverse 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) {
        if(head == NULL) return head;
        ListNode *pre = head,*next = pre->next,*temp;
        while(next){
            temp = next->next;
            next->next = pre;
            pre = next;
            next = temp;
        }
        head->next = NULL;
        return pre;
    }
};

 

posted @ 2015-05-13 22:14  SprayT  阅读(60)  评论(0编辑  收藏  举报