[LeetCode] Reverse Lists

简介: Well, since the head pointer may also be modified, we create a new_head that points to it to facilitate the reverse process.

Well, since the head pointer may also be modified, we create a new_head that points to it to facilitate the reverse process.

For the example list 1 -> 2 -> 3 -> 4 -> 5 in the problem statement, it will become 0 -> 1 -> 2 -> 3 -> 4 -> 5 (we init new_head -> val to be 0). Then we set a pointer pre to new_head and another cur to head. Then we keep inserting cur -> next after pre until cur becomes the last node. The code is follows.

 1 class Solution {
 2 public:
 3     ListNode* reverseList(ListNode* head) {
 4         ListNode* new_head = new ListNode(0);
 5         new_head -> next = head;
 6         ListNode* pre = new_head;
 7         ListNode* cur = head;
 8         while (cur && cur -> next) {
 9             ListNode* temp = pre -> next;
10             pre -> next = cur -> next;
11             cur -> next = cur -> next -> next;
12             pre -> next -> next = temp;
13         }
14         return new_head -> next;
15     }
16 };

This link provides a more concise solution without using the new_head. The idea is to reverse one node at a time for the beginning of the list. The rewritten code is as follows.

 1 class Solution {
 2 public:
 3     ListNode* reverseList(ListNode* head) {
 4         ListNode* pre = NULL;
 5         while (head) {
 6             ListNode* next = head -> next;
 7             head -> next = pre;
 8             pre = head;
 9             head = next;
10         }
11         return pre;
12     }
13 };

Well, both of the above solutions are iterative. The hint has also suggested us to use recursion. In fact, the above link has a nice recursive solution, whose rewritten code is as follows.

 1 class Solution {
 2 public:
 3     ListNode* reverseList(ListNode* head) {
 4         if (!head || !(head -> next)) return head;
 5         ListNode* node = reverseList(head -> next);
 6         head -> next -> next = head;
 7         head -> next = NULL;
 8         return node;
 9     }
10 };

The basic idea of this recursive solution is to reverse all the following nodes after head. Then we need to set head to be the final node in the reversed list. We simply set its next node in the original list (head -> next) to point to it and sets its next to be NULL.

目录
相关文章
|
机器学习/深度学习 NoSQL 算法
LeetCode 344. 反转字符串 Reverse String
LeetCode 344. 反转字符串 Reverse String
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 206. 反转链表 Reverse Linked List
LeetCode 206. 反转链表 Reverse Linked List
|
机器学习/深度学习 NoSQL
LeetCode 344. Reverse String
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
70 0
LeetCode 344. Reverse String
LeetCode 190. Reverse Bits
颠倒给定的 32 位无符号整数的二进制位。
65 0
LeetCode 190. Reverse Bits
LeetCode之Merge Two Sorted Lists
LeetCode之Merge Two Sorted Lists
87 0
LeetCode之Reverse String II
LeetCode之Reverse String II
85 0
LeetCode之Reverse String
LeetCode之Reverse String
71 0
LeetCode之Reverse Integer
LeetCode之Reverse Integer
78 0
|
索引
LeetCode 599: 两个列表的最小索引总和 Minimum Index Sum of Two Lists
题目: 假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。 Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅。
850 0

热门文章

最新文章