这题很简单,用双指针将链表反转,如果新建一个额外链表就太耗费空间了,直接在原链表上进行操作会快的多;
struct ListNode* reverseList(struct ListNode* head)
{
ListNode* temp; // 保存cur的下一个节点
ListNode* cur = head;
ListNode* pre = NULL;
while (cur)
{
temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}