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

天水建设银行网站青海公路建设信息服务网站

天水建设银行网站,青海公路建设信息服务网站,小网站搜什么关键词,网站建设交流材料目录 一、移除链表元素二、设计链表三、反转链表四、两两交换链表中的节点五、删除链表倒数第N个节点六、链表相交七、环形链表Ⅱ 一、移除链表元素 Leetcode 203 class Solution { public:ListNode* removeElements(ListNode* head, int val) {ListNode* dummyHead new Lis… 目录 一、移除链表元素二、设计链表三、反转链表四、两两交换链表中的节点五、删除链表倒数第N个节点六、链表相交七、环形链表Ⅱ 一、移除链表元素 Leetcode 203 class Solution { public:ListNode* removeElements(ListNode* head, int val) {ListNode* dummyHead new ListNode(0);dummyHead-next head;ListNode* cur dummyHead;while (cur-next ! nullptr) {if (cur-next-val val) {ListNode* tmp cur-next;cur-next tmp-next;delete tmp;} else cur cur-next;}head dummyHead-next;delete dummyHead;return head;} };二、设计链表 Leetcode 707 class MyLinkedList { public:struct LinkedNode {int val;LinkedNode* next;LinkedNode(int val): val(val), next(nullptr) {}};MyLinkedList() {_dummyHead new LinkedNode(0);_size 0;}int get(int index) {if (index (_size - 1) || index 0) return -1;LinkedNode* 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);LinkedNode* cur _dummyHead;while (cur-next ! nullptr) cur cur-next;cur-next newNode;newNode-next nullptr;_size ;}void addAtIndex(int index, int val) {if (index _size) return;if (index 0) index 0;LinkedNode* newNode new LinkedNode(val);LinkedNode* 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;LinkedNode* cur _dummyHead;while (index -- ) cur cur-next;LinkedNode* tmp cur-next;cur-next tmp-next;delete(tmp);_size -- ;}private:int _size;LinkedNode* _dummyHead; };三、反转链表 Leetcode 206 双指针法 class Solution { public:ListNode* reverseList(ListNode* head) {ListNode *tmp, *cur head, *pre nullptr;while (cur) {tmp cur-next;cur-next pre;pre cur, cur tmp;}return pre;} };递归法 class Solution { public:ListNode* reverse(ListNode* pre, ListNode* cur) {if (cur nullptr) return pre;ListNode* tmp cur-next;cur-next pre;return reverse(cur, tmp);}ListNode* reverseList(ListNode* head) {return reverse(nullptr, head);} };头插法 class Solution { public:ListNode* reverseList(ListNode* head) {ListNode* dummy new ListNode(-1);dummy-next nullptr;ListNode* cur head;while (cur ! nullptr) {ListNode* tmp cur-next;cur-next dummy-next;dummy-next cur;cur tmp;}return dummy-next;} };使用栈来反转链表 class Solution { public:ListNode* reverseList(ListNode* head) {if (head nullptr) return nullptr;if (head-next nullptr) return head;stackListNode* stk;ListNode* cur head;while (cur ! nullptr) stk.push(cur), cur cur-next;ListNode* dummy new ListNode(-1);cur dummy;while (!stk.empty()) {ListNode *node stk.top(); stk.pop();cur-next node;cur cur-next;}cur-next nullptr;return dummy-next;} };四、两两交换链表中的节点 Leetcode 24 class Solution { public:ListNode* swapPairs(ListNode* head) {ListNode* dummy new ListNode(-1);dummy-next head;ListNode* cur dummy;while (cur-next ! nullptr cur-next-next ! nullptr) {ListNode* tmp cur-next-next-next;ListNode* tmp1 cur-next;cur-next cur-next-next;cur-next-next tmp1;cur-next-next-next tmp;cur cur-next-next;}return dummy-next;} };五、删除链表倒数第N个节点 Leetcode 19 class Solution { public:ListNode* removeNthFromEnd(ListNode* head, int n) {ListNode* dummy new ListNode(-1);dummy-next head;ListNode *slow dummy, *fast dummy;while (n -- fast ! nullptr) fast fast-next;fast fast-next; // fast 多走一步last少走一步到被删除节点的前一个节点方便删除while (fast ! nullptr) fast fast-next, slow slow-next;ListNode* tmp slow-next;slow-next tmp-next;delete(tmp);return dummy-next;} };六、链表相交 面试题 02.07 双指针 class Solution { public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {if (headA nullptr || headB nullptr) return nullptr;ListNode *pa headA, *pb headB;while (pa ! pb) {pa pa nullptr ? headB : pa-next;pb pb nullptr ? headA : pb-next;}return pa;} };先统计两个链表长度再将较长链表先遍历到两个链表能尾部对其的位置再开始遍历。 class Solution { public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {ListNode *curA headA, *curB headB;int lenA 0, lenB 0;while (curA ! nullptr) curA curA-next, lenA ;while (curB ! nullptr) curB curB-next, lenB ;curA headA, curB headB;if (lenB lenA) swap(lenA, lenB), swap(curA, curB);int gap lenA - lenB;while (gap -- ) curA curA-next;while (curA ! nullptr) {if (curA curB) return curA;curA curA-next;curB curB-next;}return nullptr;} };七、环形链表Ⅱ Leetcode 142 参考题解 class Solution { public:ListNode *detectCycle(ListNode *head) {ListNode *fast head, *slow head;while (fast ! nullptr fast-next ! nullptr) {fast fast-next-next;slow slow-next;if (fast slow) {ListNode *p1 fast, *p2 head;while (p1 ! p2) p1 p1-next, p2 p2-next;return p1;}}return nullptr;} };
http://www.hkea.cn/news/14428292/

相关文章:

  • 化工网站建站模板下载免费制作音乐的软件app
  • 网站对联模板友汇网网站建设
  • 网站编排物流托运
  • 电子商务网站建设的核心多选wordpress 网盘 插件
  • 佳城建站 网站做会计公司网站的目录
  • 珠宝首饰商城网站建设无网站网络营销
  • 网站维护费进入哪个科目19年做哪个网站致富
  • flash 如何做游戏下载网站wordpress幻灯片链接
  • 网站建设夬金手指排名壹柒wordpress登陆页插件面
  • 钢管网站模板网站信息安全保障制度建设情况
  • 网站前台设计工具wordpress表单样式
  • 成都网站seo推广今天北京感染了多少人
  • 羊肉口报关做网站wordpress一键生成
  • 辽宁建设工程信息网官网新网站如何进入旅游电子商务网站开发项目进度表
  • 服务器搭建网站域名配置运城小程序开发公司
  • 咸宁网站seo怎样做能直接上传微信的视频网站
  • 北京公司网站制作流程做海外网站交税吗
  • 学校网站建设需求文档中建海峡建设发展有限公司网站
  • dw网站制作的源代码网站建设智能优化
  • 大连网页模板建站wordpress pdo mysql扩展
  • 玉环县企业网站建设门户网站建设 总结
  • 央企网站群建设wordpress区块链快讯模板
  • wordpress绑定网站北京房地产开发商排名
  • 做网站写概要设计wordpress -editor
  • wap网站如何建设运城手机网站建设
  • 网站空间搭建网站做动态图片不显示
  • 制作企业网站需要注意的事项常熟经济开发区人才网
  • 天津网站建设基本流程企业网站如何进行定位
  • 成都网站建设 3e网络自动推广软件下载
  • 阿里云 备案 网站服务内容北京市市场监督管理网上服务平台