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

门户网站源码入驻WordPress点击logo返回首页

门户网站源码入驻,WordPress点击logo返回首页,做360手机网站优化,深圳百度关键词● 自己看到题目的第一想法 203.移除链表元素 方法一#xff1a; 思路#xff1a; 设置虚拟头节点 dummyhead 设置临时指针 cur 遍历 整个链表 循环#xff1a; 如果 cur !nullptr cur-next !nullptr 则 遍历链表 否则结束遍历 如果 cur-next val 则…● 自己看到题目的第一想法 203.移除链表元素 方法一 思路 设置虚拟头节点 dummyhead 设置临时指针 cur 遍历 整个链表 循环 如果 cur !nullptr cur-next !nullptr 则 遍历链表 否则结束遍历 如果 cur-next val 则 cur-next cur-next-next 如果 cur-next !val 则 cur cur-next 返回 return dummyhead-next 注意用while循环代码 /*** 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.头节点是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; 注意 代码 /*** 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;} };运行结果 707.设计链表 思路注意: cur应该指向_dummyhead 还是_dummyhead-next; 链表的构造struct 还有 private中 链表的 定义代码 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) || index0){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(index0) 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(index0 || 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);*/ 运行结果 206.反转链表 方法一 思路双指针 定义pre null, cur head, 临时变量temp保存 cur-next; 循环 cur ! null让cur-next pre; pre cur; cur temp;返回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 nullptr;ListNode* tmp ;while(cur !nullptr){tmp cur-next;cur-next pre ;pre cur;cur tmp;}return pre;} };运行结果 方法二 思路递归法 先完成翻转的第一步: 确定终止条件 curnull 返回 pre 循环体 cur -next pre 递归下去 return reverse(cur, tmp) 注意 代码 /*** 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);} };运行结果
http://www.w-s-a.com/news/647270/

相关文章:

  • 手机网站设计需要学什么wordpress读法
  • WordPress pajx天津短视频seo
  • 检察院门户网站建设情况总结深圳网站制作长沙
  • 单页导航网站模板搜索量查询
  • 如何在一个地方建设网站营销型定制网站
  • 保定网站建设方案维护动易网站中添加邮箱
  • 简易网站的html代码wordpress音乐html
  • 四川住房和城乡建设厅网站打不开海山网站建设
  • 深圳设计功能网站如何用html制作网站
  • 网络优化软件下载竞价排名和seo的区别
  • 龙华新区做网站中高端网站建设
  • 网站开发小图标大全手机网站设计开发
  • 网页设计设计一个网站口碑营销的优点
  • 枣庄建网站的公司唐山企业网络推广培训
  • 张家界建设企业网站学校资源网站建设方案
  • 网站制作教程书籍业务管理系统
  • 上传网站空间的建站程序怎么删除c 网站开发案例详解下载
  • 企业网站维护兼职丹阳网站优化
  • 秦皇岛网站开发公司怎么注册自己的公司
  • 写作网站哪个能得稿费绿色环保企业网站模板
  • 牡丹江网站建设定制开发安徽建设工程信息网官网入口
  • 有什么好的网站建设的书适合在家做的网站工作
  • wordpress情侣源码西安网站快速优化
  • 昆明网站建设高端定制100种班服设计图
  • 网站开发程序说明html网页制作接单
  • 企业网站货物查询怎么做制作文件的软件
  • 怎么做网站的防盗链北京门户企业网站建设
  • 网站推广的主流方法淘客网站 源码
  • 网站海外推广怎么做多用户商城系统源码教程
  • 猎头做单网站网站创建费用