打卡第三天|力扣203.移除链表元素 、707.设计链表 、 206.反转链表 。

今天开始就进入链表的章节了,链表是一种线性表,实现链表的方式多用结构体,然后开辟动态内存。算法比赛中则一般用静态链表也就是数组模拟链表的操作,少去了类似C++里面类似new函数的操作,运行效率更快。

203.移除链表元素

链接203. 移除链表元素

移除链表元素主要有两种常用的方法,一种是在原链表进行删减,一种是利用虚拟头结点。

第一种在原链表进行删减,需要区分头节点和一般节点

如果是头节点,我们则是将这个头结点后移

 while(head != NULL && head->val == val){
         head = head->next;
       }

如果是一般节点,我们则是直接让上一个节点的指针指要删除节点的下一个节点。

 

 auto cur = head;
       while(cur != NULL && cur->next != NULL){
           if(cur->next->val == val){
               cur->next = cur->next->next;
           }else{
               cur = cur->next;
           }
       }

完整代码如下:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
       while(head != NULL && head->val == val){
         head = head->next;
       }
       auto cur = head;
       while(cur != NULL && cur->next != NULL){
           if(cur->next->val == val){
               cur->next = cur->next->next;
           }else{
               cur = cur->next;
           }
       }
       return head;
    }
};

第二种是使用虚拟头节点,使用虚拟头节点的好处是统一了增删操作,不用再区分,头结点和非头结点。

原理跟在原链表删除一般节点的方法一样,就是设置了一个虚拟头节点方便操作。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
      auto dummy_head = new ListNode(0);
      dummy_head->next = head;
      auto cur = dummy_head;
      while(cur->next !=NULL){
          if(cur->next->val == val){
              cur->next = cur->next->next;
          }else{
              cur = cur->next;
          }
      }
//注意此时头节点位置
      head = dummy_head->next;
      return head;
    }
};

707.设计链表 

题目链接707. 设计链表

这道题就是链表的常规操作了,都是用虚拟头结点的方式实现的,然后注意头结点是算作第0个节点,然后遍历链表的时候要用cur做一个临时指针以免将头节点改变

class MyLinkedList {
public:
struct LinkedNode{
    int val;
    LinkedNode* next;
//构造函数
    LinkedNode(int val):val(val),next(nullptr){}
};
    LinkedNode*dummyhead;
    int size;
    MyLinkedList() {
    dummyhead= new LinkedNode(0);
    size=0;
    }
    
    int get(int index) {
        if (index > (size - 1) || index < 0) {
            return -1;
        }
    auto cur = dummyhead->next;
    while(index--){
        cur = cur->next;
    }
    return cur->val;
    }
    
    void addAtHead(int val) {
    LinkedNode*newnode = new LinkedNode(val);
//注意先接后面的再接前面的,这是经常犯错的地方
    newnode->next = dummyhead->next;
    dummyhead->next = newnode;
    size++;
    }
    
    void addAtTail(int val) {
     LinkedNode*newnode = new LinkedNode(val);
     auto cur =dummyhead;
     while(cur->next !=nullptr){
         cur = cur->next;
    
     }
     cur->next = newnode;
     size++;
    }
    
    void addAtIndex(int index, int val) {
        if (index > size) {
            return;
        }
      LinkedNode*newnode = new LinkedNode(val);
      auto cur = dummyhead;
      while(index--){
          cur = cur->next;
      }
      newnode->next = cur->next;
      cur->next = newnode;
      size++;
    }
    
    void deleteAtIndex(int index) {
     if (index >= size || index < 0) {
            return;
        }
        auto cur = dummyhead;
        while(index--){
            cur = cur->next;
        }
        cur->next = cur->next->next;
        size--;
    }

};

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList* obj = new MyLinkedList();
 * int param_1 = obj->get(index);
 * obj->addAtHead(val);
 * obj->addAtTail(val);
 * obj->addAtIndex(index,val);
 * obj->deleteAtIndex(index);
 */

 206.反转链表

题目链接206. 反转链表

翻转链表常用的就是双指针写法,让cur和pre指针依次往后遍历同时让cur的方向指向pre实现翻转链表,要注意的是每条赋值语句的顺序

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
    ListNode* cur = head;
    ListNode* pre = NULL;
    while(cur){
//注意语句顺序
        ListNode* temp = cur->next;
        cur->next = pre;
        pre = cur;
        cur = temp;
    }
    return pre;
    }
};

当然,也可以用递归的写法,显得厉害一点点,其实逻辑是一样的,只是代码简短一点,也难懂一点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
    ListNode* reverse(ListNode* pre,ListNode* cur){
        if(cur == NULL)return pre;
        auto temp = cur->next;
        cur->next = pre;
        return  reverse(cur,temp);
    }
public:
    ListNode* reverseList(ListNode* head) {
      return reverse(NULL,head);
    }
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr丶锤子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值