30 Day Challenge Day 6 | Hackerrank: Reverse a doubly linked list
题解
与Leetcode上翻转链表(206. Reverse Linked List)思路一致,只不过多了一个“prev”前节点的处理,这种题通过画图比较容易验证。
DoublyLinkedListNode* reverse(DoublyLinkedListNode* head) {
if(!head || !head->next) return head;
DoublyLinkedListNode* prev = nullptr;
DoublyLinkedListNode* curr = head;
while(curr) {
DoublyLinkedListNode* temp = curr->next;
curr->next = prev;
curr->prev = temp;
prev = curr;
curr = temp;
}
return prev;
}