- 思路
提示是可以用迭代和递归,感觉这个递归的条件我搞不太懂,还是用简单的迭代,大概就是每两个做一次变换,直到NULL - 错误
一开始我让newhead = head; newhead->next = temp; 没考虑到第一个要指向NULL,显示时间超过了限制
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head){
struct ListNode *temp, *newhead = NULL;
while(head!=NULL){
temp = head;
head = head->next;
temp->next = newhead;
newhead = temp;
}
return newhead;
}