利用迭代(双指针)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head) {
if(!head) //链表为空,则放回空
return NULL;
struct ListNode* cur = head;
struct ListNode* newHead = NULL;
while(cur)
{
struct ListNode* next = cur->next; // 暂存后继节点 cur.next
cur->next = newHead; // 修改 next 引用指向
newHead = cur; //更新
cur = next; //更新
}
return newHead;
}