这个题目是在学习数据结构时一定会思考过的问题,需要注意的是头结点的和空指针的处理,很简单,直接上代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
ListNode* reverseList(ListNode* head) {
if(head == NULL)
return NULL;
ListNode* back = head->next;
head->next = NULL;
while(back != NULL){
ListNode* aid = back;
back = back->next;
aid ->next = head;
head = aid;
}
return head;
}