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 NULL;
         ListNode *nhead = NULL;//nhead来保存一个新的链表
         while(head)
         {ListNode*cur = head;//处理的时候cur 是当前处理的节点,
         head= head->next;
         cur->next = nhead;//nhead是当前节点的前一个节点
         nhead = cur;//为下次循环服务
            
         }
         return nhead;
    }
};

posted @ 2015-12-10 15:14  雪之灵  阅读(102)  评论(0编辑  收藏  举报