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

思茅网站建设深圳seo优化公司排名

思茅网站建设,深圳seo优化公司排名,门户网站建设招投标,华为手机官网入口目录 一、移除链表元素二、设计链表三、反转链表四、两两交换链表中的节点五、删除链表倒数第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;stack<ListNode*> 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.mmbaike.com/news/23218.html

相关文章:

  • 做淘宝站外推广网站百度数据网站
  • 晚上做羞羞的事情视频网站智能营销系统开发
  • 苏州网站模板建站如何营销推广自己的产品
  • 网站建设合同付款比例app制作费用一览表
  • 百度网站排名规则手机一键优化
  • 咸阳北京网站建设重庆seo标准
  • 电子商务行业网站描述优化方法
  • 网站系统网站开发温州seo外包公司
  • 网页与网站的区别与联系注册公司网站
  • 天然气公司的网站应该怎么做平台推广销售话术
  • 在淘宝做网站可以退货退款么信息流优化师简历
  • 页面设计升级访问紧急通知济南网站优化公司哪家好
  • 制作企业网站的流程收录优美的图片
  • 做排名的网站搜索引擎seo优化怎么做
  • 有找猎聘网站做简历优化的外贸网站建设 google
  • 做网站超链接用什么软件中国军事新闻最新消息
  • 平昌网站建设谷歌优化
  • 公司网站公司简介宣传夸大受处罚网络营销工具与方法
  • 苏州网站建设哪家更好商品促销活动策划方案
  • 广告联盟没网站可以做吗淘宝优化关键词的步骤
  • 副业做网站程序百度学术官网登录入口
  • ps如何做网站百度热门
  • 网站代码 公告栏 php百度seo服务公司
  • 国外网站的设计风格网址查询地址查询
  • 易县做网站网络营销推广技巧
  • 手机如果做网站长沙关键词优化公司电话
  • 门户网站建设工作汇报好的竞价托管公司
  • 长沙网站建设公司名单西安百度公司开户
  • 网站首页做了一下调整会被k吗如何在手机上建立自己的网站
  • 网站的风格设计大量微信群推广代发广告