day2
1.剑指 Offer 06. 从尾到头打印链表
1)常规法,遍历+反向迭代器
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 vector<int> reversePrint(ListNode* head) { 12 vector<int> res; 13 while(head){ 14 res.push_back(head -> val); 15 head = head -> next; 16 } 17 return vector<int>(res.rbegin(),res.rend()); 18 } 19 };
2)递归法,遍历到最后刚好 从最后一个数据开始往容器里放数据
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 vector<int> res; 12 13 vector<int> reversePrint(ListNode* head) { 14 recur(head); 15 return res; 16 } 17 18 void recur(ListNode* head){ 19 if(head == nullptr) 20 return; 21 recur(head -> next); 22 res.push_back(head -> val); 23 } 24 };
2.剑指 Offer 24. 反转链表
1)
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* reverseList(ListNode* head) { 12 ListNode* p = nullptr; 13 while(head){ 14 ListNode* tmp = head -> next; 15 head -> next = p; 16 p = head; 17 head = tmp; 18 } 19 return p; 20 } 21 };
2)递归( 头节点那里是真的妙!)
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* reverseList(ListNode* head) { 12 return recur(nullptr,head);//为什么不从head开始:方便处理空链表和只有一个数据的链表 13 } 14 15 ListNode* recur(ListNode* pre,ListNode* cur){ 16 if(cur == nullptr) return pre; 17 ListNode* res = recur(cur,cur -> next); 18 cur -> next = pre;//反串 19 return res; 20 }//res的作用仅仅就是保存头节点!!! 21 };
3.剑指 Offer 35. 复杂链表的复制
1)哈希表,key:节点,value: 节点
1 /* 2 // Definition for a Node. 3 class Node { 4 public: 5 int val; 6 Node* next; 7 Node* random; 8 9 Node(int _val) { 10 val = _val; 11 next = NULL; 12 random = NULL; 13 } 14 }; 15 */ 16 class Solution { 17 public: 18 Node* copyRandomList(Node* head) { 19 if(head == nullptr) 20 return nullptr; 21 unordered_map<Node*,Node*> map; 22 Node* cur = head; 23 while(cur){ 24 map[cur] = new Node(cur -> val); 25 cur = cur -> next; 26 } 27 cur = head; 28 while(cur){ 29 map[cur] -> next = map[cur -> next]; 30 map[cur] -> random = map[cur -> random]; 31 cur = cur -> next; 32 } 33 return map[head]; 34 } 35 };
2)复制拼接+拆分
1 /* 2 // Definition for a Node. 3 class Node { 4 public: 5 int val; 6 Node* next; 7 Node* random; 8 9 Node(int _val) { 10 val = _val; 11 next = NULL; 12 random = NULL; 13 } 14 }; 15 */ 16 class Solution { 17 public: 18 Node* copyRandomList(Node* head) { 19 if(head == nullptr) 20 return nullptr; 21 Node* cur = head; 22 while(cur){ 23 Node* tmp = new Node(cur -> val); 24 tmp -> next = cur -> next; 25 cur -> next = tmp; 26 cur = tmp -> next; 27 } 28 cur = head; 29 while(cur){ 30 if(cur -> random) 31 cur -> next -> random = cur -> random -> next;//创建的新节点next和random都是nullptr,所以要设置 //I 32 cur = cur -> next -> next; 33 } 34 cur = head -> next; 35 Node* pre = head,*res = head -> next;//res保存头节点 36 while(cur -> next){ 37 pre -> next = pre -> next -> next; 38 cur -> next = cur -> next -> next; 39 pre = pre -> next; 40 cur = cur -> next; 41 } 42 pre -> next = nullptr;//处理原链表尾节点 43 return res; 44 } 45 }; 46 /* 47 cur = head -> next; //II 48 Node* pre = head,*res = head -> next;//res保存头节点 49 while(cur -> next){ 50 cur -> random = pre -> random -> next;//创建的新节点next和random都是nullptr,所以要设置 51 pre -> next = pre -> next -> next; 52 cur -> next = cur -> next -> next; 53 pre = pre -> next; 54 cur = cur -> next; 55 } 56 */
I.注意第31行:cur->next->random = cur -> random 是错误的,cur ->next->random保存的random值应该是cur->random保存的值即 cur->random->next
II.注意47-55行,我之前是想把28-41行改为47-55行,发现不行,因为最后一个(cur)节点会分配不到random值

浙公网安备 33010602011771号