当前位置: 首页 > news >正文

淮南网站建设好的公司专业seo优化公司

淮南网站建设好的公司,专业seo优化公司,绵阳市做公司网站,做网站的工作叫什么● 自己看到题目的第一想法 203.移除链表元素 方法一: 思路: 设置虚拟头节点 dummyhead 设置临时指针 cur 遍历 整个链表 循环: 如果 cur !nullptr &&cur->next !nullptr 则 遍历链表 否则结束遍历 如果 cur->next val 则…

● 自己看到题目的第一想法

203.移除链表元素

方法一:

  1. 思路:
    设置虚拟头节点 dummyhead
    设置临时指针 cur 遍历 整个链表
    循环:
  • 如果 cur !=nullptr &&cur->next !=nullptr 则 遍历链表 否则结束遍历

  • 如果 cur->next == val 则 cur->next = cur->next->next

  • 如果 cur->next !=val 则 cur = cur->next

返回 return dummyhead->next

  1. 注意:用while循环
  2. 代码:
/*** 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) {ListNode* dummyhead = new ListNode(0);dummyhead->next = head;ListNode* cur = dummyhead;while(cur !=nullptr &&cur->next !=nullptr){if(cur->next->val == val){cur->next = cur->next->next;}else{cur = cur->next;}}head = dummyhead->next;delete dummyhead;return head;}
};
  1. 运行结果:
    在这里插入图片描述

方法二:

  1. 思路:
    直接在原链表上操作

    1.头节点是val值
    删除头节点 head = head->next;

    2.头节点不是val值
    定义一个临时变量cur 遍历整个链表
    循环 :

  • 如果cur !=nullptr && cur->next !=nullptr 则 遍历链表 否则结束遍历

  • 如果 cur->next == val 则 cur->next = cur->next->next

  • 如果 cur->next !=val 则 cur = cur->next

返回 return head;

  1. 注意:

  2. 代码:

/*** 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 !=nullptr && head->val == val){head = head->next;}ListNode *cur = head;while(cur !=nullptr && cur->next !=nullptr){if(cur->next->val == val ){cur->next = cur->next->next;}else{cur = cur->next;}}return head;}
};
  1. 运行结果:

在这里插入图片描述

707.设计链表

  1. 思路:
  2. 注意:
    cur应该指向_dummyhead 还是_dummyhead->next;
    链表的构造struct 还有 private中 链表的 定义
  3. 代码:
class MyLinkedList {
public:
struct ListNode{int val;ListNode* next ;ListNode(int val): val(val), next(nullptr){}
};MyLinkedList() {_size = 0;_dummyhead = new ListNode(0);}int get(int index) {if(index>(_size-1) || index<0){return -1;}ListNode* cur = _dummyhead;while(index){cur = cur->next;index--;}return cur->next->val;}void addAtHead(int val) {ListNode* newnode = new ListNode(val);newnode->next = _dummyhead->next;_dummyhead->next = newnode;_size++;}void addAtTail(int val) {ListNode* cur = _dummyhead;ListNode* newnode = new ListNode(val);while(cur !=nullptr && cur->next !=nullptr){cur =cur->next;}cur->next = newnode;_size++;}void addAtIndex(int index, int val) {ListNode* newnode = new ListNode(val);if(index<0)  index =0;if(index >_size) return ;ListNode * cur = _dummyhead;while(index--){cur = cur->next;}newnode->next = cur->next;cur->next = newnode;_size++;}void deleteAtIndex(int index) {if(index<0 || index>(_size-1)){return ;}ListNode*cur = _dummyhead;while(index--){cur = cur->next;}cur->next = cur->next->next;_size--;}private:int _size;ListNode* _dummyhead;
};/*** 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);*/
  1. 运行结果:
    在这里插入图片描述

206.反转链表

方法一:

  1. 思路:双指针
    定义pre= null, cur = head, 临时变量temp保存 cur->next;
    循环:

     cur != null让cur->next = pre;   pre = cur; cur = temp;
    

    返回:pre

  2. 注意:

  3. 代码:

/*** 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 = nullptr;ListNode* tmp ;while(cur !=nullptr){tmp = cur->next;cur->next = pre ;pre  =cur;cur = tmp;}return pre;}
};
  1. 运行结果
    在这里插入图片描述
    方法二:

  2. 思路:递归法:

    先完成翻转的第一步:
    确定终止条件: cur==null 返回 pre
    循环体: cur ->next = pre
    递归下去 return reverse(cur, tmp)

  3. 注意:

  4. 代码:

/*** 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* reverse(ListNode* pre, ListNode* cur ){if(cur == nullptr) return pre;ListNode* temp;temp = cur->next;cur->next = pre;return reverse(cur, temp);}ListNode* reverseList(ListNode* head) {return reverse(nullptr, head);}
};
  1. 运行结果:
    在这里插入图片描述
http://www.dt0577.cn/news/28.html

相关文章:

  • 做的网站怎样评估价值云南最新消息
  • 企业电话查询google seo 优化
  • 个人网站网页模板友情链接交换群
  • 深圳做网站的爱情独白智能搜索引擎
  • 哪个网站可以做相册阿里云搜索
  • 网站如何做微信支付宝支付宝支付济南疫情最新情况
  • 如何建设简易网站seo的培训网站哪里好
  • 用群晖做网站网站统计哪个好用
  • 手机网站效果图做多大的推广管理
  • 做一个电商网站多少钱纯注册app拉新平台
  • 安徽网站定制seo电商运营是什么意思
  • 电话销售-网站建设-开场白网站建设的系统流程图
  • 建设专业网站价格曲靖seo
  • 网站设计与网页建设新东方考研班收费价格表
  • wordpress调用会员等级广告投放优化师
  • 如何做网站页面免费的什么是优化师
  • 四大门户网站现状网站seo优化方案设计
  • wordpress 显示word文档win7优化大师官方网站
  • 网站开发有哪些方式东莞网站建设公司
  • 丹阳网站建设咨询百度网址安全检测中心
  • 淘宝网站建设的主要工作深圳专业建站公司
  • 点击立即进入正能量网站网站手机版排名seo
  • 微做网站百度推广获客成本大概多少
  • 用vue做的网站seo教学
  • 建设厅网站账号密码忘记怎么办关键词如何确定
  • 电子商务免费网站建设长春seo外包