标签:
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) { if(head == NULL) return NULL; ListNode* node; node = (ListNode*)malloc(sizeof(ListNode*)); node->next = NULL; while(head){ ListNode* ptr = head; head = head->next; ptr->next = node->next; node->next = ptr; } return node->next; } };
标签:
原文地址:http://www.cnblogs.com/horizonice/p/4770510.html