【
反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL
】
链表题经常需要申请一个虚拟节点,此题便是。
struct ListNode* reverseList(struct ListNode* head){
struct ListNode* newp;
struct ListNode* curp = head;
struct ListNode* top = head;
if (head == NULL) return NULL;
newp = (ListNode*)malloc(sizeof(struct ListNode));
while(head) {
newp->next = top;
head = head->next;
top->next = curp;
curp = top;
top = head;
}
head = newp->next;
return head;
}