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

第二章 链表

目录

  • 一、移除链表元素
  • 二、设计链表
  • 三、反转链表
  • 四、两两交换链表中的节点
  • 五、删除链表倒数第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.lryc.cn/news/68416.html

相关文章:

  • Spring Security OAuth2实现单点登录:简化多个系统之间的登录流程
  • 语义分析器
  • 爬虫基本原理
  • 常见电子元器件和电路
  • English Learning - L3 Lesson1 VOA-Color 译文
  • 如何在linux中配置JDK环境变量
  • 横截面收益率(二) 阿尔法策略是如何构建的
  • 【ConfluxNews】2023.5.15 警惕任何未经合约审计的项目
  • MySQL学习---17、MySQL8其它新特性
  • 快速入门matlab——变量练习
  • c++ 11标准模板(STL) std::set(三)
  • ChatGPT详细介绍
  • 【算法】【算法杂谈】让[0,x)区间上的出现概率变为x^k
  • 【2023华为OD笔试必会25题--C语言版】《21 对称美学》——字符串、递归
  • 为减少来自环境使用的无线传感器网络的传输次数而开发的方法(Matlab代码实现)
  • springboot+vue滴答拍摄影项目(源码+文档)
  • SQL基础培训13-索引和优化
  • 拥抱5G发展机遇,从边缘计算上车
  • “前端”工匠系列(二):合格的工匠,怎么做好价值落地 | 京东云技术团队
  • Oracle11g下载与安装
  • 考研复试-软件工程
  • 软件测试选择题
  • 有限合伙企业与有限公司的区别
  • 从洛克菲勒思想中洞悉的财富秘密
  • 如何训练自己的大型语言模型
  • Java中的SLF4J是什么?如何使用SLF4J进行日志管理
  • PHP程序员面对的压力大不大?我来聊聊程序员转行的就业方向
  • 牛客网专项练习Pytnon分析库(十)
  • leecode654——最大二叉树
  • 【笔试强训选择题】Day12.习题(错题)解析