三指针法
ListNode *reverse(ListNode *head) {
// write your code here
if(head == NULL) return NULL;
if(head->next == NULL) return head;
ListNode* begin = head;
ListNode* last = head->next;
ListNode* cur = head;
while(last)
{
cur = last;
last = last->next;
cur->next = begin;
begin = cur;
}
head->next = NULL;
return cur;
}
每日一题——逆置单链表
最新推荐文章于 2023-01-07 18:23:37 发布