链表

 1、移除链表元素(203)

移除时是需要三个节点协同,但当头节点需要删除时,难以统一;因此需要在头节点前面设置哑节点,从而保持一致。

依旧使用双指针 cur和 pre 分别是dump和head;

  如果相同 cur->next = pre-next; pre = pre->next;

  否则  cur = pre; pre = pre->next;

在返回时注意是 return dump->next;

 1 class Solution {
 2 public:
 3     ListNode* removeElements(ListNode* head, int val) {
 4 
 5         ListNode* dump = new ListNode(0);
 6         dump->next = head;
 7 
 8         ListNode* pre = head;
 9         ListNode* cur = dump;
10 
11         while(pre!=NULL){
12             if(pre->val == val){
13                 cur->next = pre->next;
14             }
15             else {
16                 cur = pre;
17             }
18             
19             pre = pre->next;
20         }
21         return dump->next;
22     }
23 };
View Code

2、反转链表(206)

pre指针在前,cur指针在后,tmp指针保存pre->next;

pre和cur先后移动;

 1 class Solution {
 2 public:
 3     ListNode* reverseList(ListNode* head) {
 4 
 5         ListNode* pre = head;
 6         ListNode* cur = NULL;
 7 
 8         while(pre != NULL){
 9             ListNode* tmp = pre->next;
10             pre->next = cur;
11             cur = pre;
12             pre = tmp;
13         }
14     return cur;
15 
16     }
17 };
View Code

3、删除链表中的节点(237)

题目精妙,解法是将下一节点的值复制到该节点,删除下一节点。

1 class Solution {
2 public:
3     void deleteNode(ListNode* node) {
4         
5         node->val = node->next->val;
6         node->next = node->next->next;
7 
8     }
9 };
View Code

4、从尾到头打印链表(剑指 Offer 06.)

栈的思想:正序遍历,反向输出

使用vector容器,调用reverse函数(a.begin(), a.end)

 1 class Solution {
 2 public:
 3     vector<int> reversePrint(ListNode* head) {
 4 
 5         vector<int> res;
 6 
 7         while(head != NULL){
 8             res.push_back(head->val);
 9             head = head->next;
10         }
11 
12         reverse(res.begin(), res.end());
13         
14         return res;
15     }
16 };
View Code

 5、相交链表

你们之所以相遇,正是因为你走了 ta 走过的路,而 ta 也刚好走了你走过的路。

当不相交时,也会在NULL处相等

 1 class Solution {
 2 public:
 3  ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
 4         if(headA==NULL || headB==NULL)
 5             return NULL;
 6         
 7         ListNode *A = headA;
 8         ListNode *B = headB;
 9 
10         while(A!=B){
11             if(A!=NULL)
12                 A = A->next;
13             else
14                 A = headB;
15             
16             if(B!=NULL)
17                 B = B->next;
18             else
19                 B = headA;
20         }
21 
22         return A;
23         
24     }
25 };
View Code

 6、合并两个有序链表

需要创建哨兵节点

注意最后结尾的合并

 1 class Solution {
 2 public:
 3     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
 4         // 判断是否为空
 5         if(l1 == NULL)return l2;
 6         if(l2 == NULL)return l1;
 7 
 8         // 哨兵
 9         ListNode *Header = new ListNode(0);
10         ListNode *tmp = Header;
11 
12         while(l1!=NULL && l2!=NULL){
13         // 选择下一节点
14         if(l1->val < l2->val){
15             tmp->next = l1;
16             l1 = l1->next;
17         }
18         else {
19             tmp->next = l2;
20             l2 = l2->next;
21         }
22         // 下一节点
23         tmp = tmp->next;
24         }
25 
26         // 结尾
27         tmp->next = l1==NULL ? l2 : l1;
28 
29         return Header->next; 
30 
31 
32 
33     }
34 };
View Code

 

posted @ 2020-08-26 16:27  Gumpest  阅读(48)  评论(0)    收藏  举报