“`”

考察点:链表

<pre><code class=""language-c"" lang=""c"">ListNode
reverseList(ListNode* head) {
if(head == nullptr || head->next ==nullptr)
return head;
ListNode* p;
ListNode* q;
ListNode* r;
p = head;
q = head->next;
head->next = nullptr;//旧的头指针是新的尾指针 指向NULL
while(q){
r = q->next;//用来保存下一步要处理的指针
q->next = p;//p q 交替处理 进行反转单链表
p = q;
q = r;
}
head = p;//最后的q必定指向NULL,p就成了新链表的头指针
return head;
}

</code></pre>

 

 

<pre><code> "“`

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.