函数模型:
ListNode* reverseLinkedList(ListNode* head)
几个case:
head是NULL
head的next是NULL
大概思路:
要有个pre,一开始set成NULL,
记下来current节点的next节点,
把current节点指向pre
cur节点变成之前记下来的当前节点
ListNode* reverseLinkedList(ListNode* head)
{
ListNode* pre = NULL;
ListNode* next = NULL;
while(head)
{
next = head->next;
head->next = pre;
pre = head;
head = next;
}
return pre;
}
网友评论