这里记录一些我刷题的思路方便之后进行复习重温,同时也方便进行添加

P141-环形链表

class Solution {
public:
bool hasCycle(ListNode *head) {
if(!head) return false;
if(!head->next) return false;
ListNode* last = reverse(head);
return last==head;
} ListNode* reverse(ListNode* head){
ListNode *pre=NULL,*curr=head;
while(curr){
ListNode* next = curr->next;
curr->next=pre;
pre=curr;
curr=next;
}
return pre;
}
};

使用翻转链表,判断返回的指针与传进的指针是否相同,时间复杂度是O(N)(?可能),空间复杂度是O(1).

class Solution {
public:
bool hasCycle(ListNode *head) {
if(!head||!head->next) return false;
ListNode *fast=head->next,*slow=head;
while(fast!=slow){
if(!fast||!fast->next) return false;
fast=fast->next->next;
slow=slow->next;
}
return true;
}
};

使用快慢指针Floyd 判圈算法,如果有环则兔子会与龟碰上,但没有环则会到NULL,这里有一个小知识点就是!fast||!fast->next这个条件需要先判断fast是否为空,顺序不能翻转否则可能进入死循环,因为判断的顺序从左到右。

p19-删除链表的倒数第N个节点

class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
head=reverse(head);
int i=1;
ListNode* ptr=head;
if(i==n) return reverse(head->next);
i++;
while(ptr&&ptr->next){
if(i==n){
ptr->next=ptr->next->next;
break;
}
ptr=ptr->next;
i++;
}
return reverse(head);
} ListNode* reverse(ListNode* head){
ListNode* pre=NULL,*curr=head;
while(curr){
ListNode* next = curr->next;
curr->next=pre;
pre=curr;
curr=next;
}
return pre;
}
};

两次翻转。

class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *slow=head, *fast=head;
while(n){
fast=fast->next;
n--;
}
if(!fast) return head->next;
while(fast->next){
fast=fast->next;
slow=slow->next;
}
slow->next=slow->next->next;
return head;
}
};

快慢指针,将最后一段的间隔设定好,即快指针先跑结尾长度,然后双指针齐头并进。

Leetcode习题集-链表的更多相关文章

  1. Leetcode解题-链表(2.2.0)基础类

    1 基类的作用 在开始练习LeetCode链表部分的习题之前,首先创建好一个Solution基类,其作用就是: Ø  规定好每个子Solution都要实现纯虚函数test做测试: Ø  提供了List ...

  2. LeetCode 单链表专题 (一)

    目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...

  3. 【算法题 14 LeetCode 147 链表的插入排序】

    算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # ...

  4. 关于leetcode中链表中两数据相加的程序说明

    * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ ...

  5. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

  6. 关于LeetCode上链表题目的一些trick

    最近在刷leetcode上关于链表的一些高频题,在写代码的过程中总结了链表的一些解题技巧和常见题型. 结点的删除 指定链表中的某个结点,将其从链表中删除. 由于在链表中删除某个结点需要找到该结点的前一 ...

  7. Leetcode中单链表题总结

    以下是个人对所做过的LeetCode题中有关链表类型题的总结,博主小白啊,若有错误的地方,请留言指出,谢谢. 一.有关反转链表 反转链表是在单链表题中占很大的比例,有时候,会以各种形式出现在题中,是比 ...

  8. LeetCode之链表总结

    链表提供了高效的节点重排能力,以及顺序性的节点访问方式,并且可以通过增删节点来灵活地调整链表的长度.作为一种常用的数据结构,链表内置在很多高级编程语言里面.既比数组复杂又比树简单,所以链表经常被面试官 ...

  9. leetcode 876. 链表的中间结点 签到

    题目: 给定一个带有头结点 head 的非空单链表,返回链表的中间结点. 如果有两个中间结点,则返回第二个中间结点. 示例 1: 输入:[1,2,3,4,5] 输出:此列表中的结点 3 (序列化形式: ...

  10. leetcode 反转链表部分节点

    反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明:1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5->NULL, m = 2, ...

随机推荐

  1. vue二级联动 编辑

    第一步先写布局:  然后写我们的二级联动的方法 getOptionsA() { this.$axios .get('http://localhost:55629/api/GetClassifies?p ...

  2. Android学习——控件ProgressBar

    1.常用属性

  3. shell脚本变量加减

    #! /bin/bash test_1=1 test_2=2 test = $(expr $test_1 - $test_2)  #减法 test = $(expr $test_1 + $test_2 ...

  4. AXI 协议翻译介绍

    一.介绍 Introduction 本章描述了axis协议的体系结构和协议定义的基本事务.它包含以下部分:•第1-2页关于AXI协议•第1-3页是架构•第1-7页是基本事务•第1-11页的附加功能. ...

  5. C#消息泵探索(二)

    ​ 引言: 上篇文章里简单的解释了C#的消息泵原理,这里我们以winform为例详细地了解一下实现代码. 底层实现 [DllImport(ExternDll.User32, ExactSpelling ...

  6. 13-之容器资源需求、资源限制及Metric-server(Heapster)

    目录 容器资源需求.资源限制及Heapster Heapster 资源指标API及自定义指标API k8s-promtheus监控部署 node-exporter prometheus kube-st ...

  7. 【git】3.4 git分支-分支开发工作流

    资料来源 (1) https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E5%BC%80%E5%8F%91% ...

  8. django连接ubuntu22下的mysql8

    1.安装mysql(这里就不过多赘述了) sudo apt-get install mysql-server 2.登录mysql   (1) 在 根目录/etc/mysql/debian.cnf ,使 ...

  9. python学习:窗口程序

    https://www.cnblogs.com/zyg123/p/10385456.html # 导入tkinter模块 import tkinter # 创建画布需要的库 from matplotl ...

  10. 使用pip安装PySide6

    https://www.perfcode.com/p/pip-install-pyside6.html 要求 在安装PySide6之前,你必须先安装Python 3.6 以上版本: 安装PySide6 ...