当前位置:网站首页>leetcode--链表
leetcode--链表
2022-06-24 08:03:00 【雨幕丶】
链表:单链表、双链表、循环链表。
链表和数组区别:
| 添加/删除 | 查找 | 适用场景 | |
| 数组 | O(n) | O(1) | 数据固定,频繁查找,增删少 |
| 链表 | O(1) | O(n) | 数据不固定,频繁增删,查找少 |
203 移除链表元素
c++使用链表删除元素后要delete该元素的内存空间
方法一:如果头节点是需要删除的元素,需要将head = head->next
ListNode* removeElements(ListNode* head, int val) {
//val是头节点元素
while (head != NULL && head->val == val)
{
ListNode* tmp = head;
head = head->next;
delete tmp;
}
ListNode* p = head;
//val不是头节点元素
while(p != NULL && p->next != NULL){
if(p->next->val == val){
ListNode* tmp = p->next;
p->next = p->next->next;
delete tmp;
}else{
p = p->next;
}
}
return head;
}方法二: 设置一个虚拟头节点(需要初始化),让这个虚拟头节点的next = head,最后记得让head = 虚拟头节点的next,然后删除虚拟头节点
ListNode* removeElements(ListNode* head, int val) {
//设置一个虚拟头节点
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* p = dummyHead; //p是当前所访问的节点
while(p->next != NULL){
if(p->next->val == val){
ListNode* tmp = p->next;
p->next = p->next->next;
delete tmp;
}else{
p = p->next;
}
}
//最后删除设置的虚拟头节点
head = dummyHead->next;
delete dummyHead;
return head;
}707 设计链表
对于数组遍历只需要知道它的长度,对于链表长度未知,只能一个一个节点去找,直到某个节点的next = null
class MyLinkedList {
public:
// 定义链表节点结构体
struct LinkedNode {
int val;
LinkedNode* next;
LinkedNode(int val):val(val), next(nullptr){}
};
// 初始化链表
MyLinkedList() {
_dummyHead = new LinkedNode(0); // 这里定义的头结点 是一个虚拟头结点,而不是真正的链表头结点
_size = 0;
}
// 获取到第index个节点数值,如果index是非法数值直接返回-1, 注意index是从0开始的,第0个节点就是头结点
int get(int index) {
if (index > (_size - 1) || index < 0) {
return -1;
}
LinkedNode* cur = _dummyHead->next;
while(index--){ // 如果--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;
_size++;
}
// 在第index个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。
// 如果index 等于链表的长度,则说明是新插入的节点为链表的尾结点
// 如果index大于链表的长度,则返回空
void addAtIndex(int index, int val) {
if (index > _size) {
return;
}
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur = _dummyHead;
while(index--) {
cur = cur->next;
}
newNode->next = cur->next;
cur->next = newNode;
_size++;
}
// 删除第index个节点,如果index 大于等于链表的长度,直接return,注意index是从0开始的
void deleteAtIndex(int index) {
if (index >= _size || index < 0) {
return;
}
LinkedNode* cur = _dummyHead;
while(index--) {
cur = cur ->next;
}
LinkedNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
_size--;
}
// 打印链表
void printLinkedList() {
LinkedNode* cur = _dummyHead;
while (cur->next != nullptr) {
cout << cur->next->val << " ";
cur = cur->next;
}
cout << endl;
}
private:
int _size;
LinkedNode* _dummyHead;
};206 反转链表
双指针法:定义cur指针指向头节点,pre指针初始化为null,记录tmp = cur->next 和cur->next = pre,更新tmp和cur。
ListNode* reverseList(ListNode* head){
ListNode* cur = head;
ListNode* pre = nullptr;
ListNode* tmp;
while(cur){
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
} 24 两两交换链表中的节点
ListNode* swapPairs(ListNode* head) {
ListNode* dummyhead = new ListNode(0);
dummyhead->next = head;
ListNode* cur = dummyhead;
while(cur->next != nullptr && cur->next->next != nullptr){
ListNode* tmp1 = cur->next;
ListNode* tmp2 = cur->next->next->next;
cur->next = cur->next->next;
cur->next->next = tmp1;
cur->next->next->next = tmp2;
cur = cur->next->next;
}
return dummyhead->next;
}19 删除链表倒数第n个节点
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* slow = dummyHead;
ListNode* fast = dummyHead;
//fast指针先移动n步
while (n-- && fast->next != nullptr){
fast = fast->next;
}
// fast再提前走一步,因为需要让slow指向删除节点的上一个节点
fast = fast->next;
while(fast != nullptr){
fast = fast->next;
slow = slow->next;
}
slow->next = slow->next->next;
return dummyHead->next;
}142链表找环
ListNode *detectCycle(ListNode *head) {
ListNode* fast = head;
ListNode* slow = head;
while(fast!= NULL && fast->next != NULL){
fast = fast->next->next;
slow = slow->next;
if(fast == slow){
ListNode* index1 = fast;
ListNode* index2 = head;
while(index1 != index2){
index1 = index1->next;
index2 = index2->next;
}
return index1;
}
}
return NULL;
}边栏推荐
- PM2 deploy nuxt3 JS project
- Go 语言项目开发实战目录
- Unable to change the virtual machine power status and report an error solution
- Data middle office: detailed explanation of technical architecture of data middle office
- Data midrange: detailed explanation of the technical stack of data acquisition and extraction
- Pytoch read data set (two modes: typical data set and user-defined data set)
- eBanb B1手环刷固件异常中断处理
- From the Huawei weautomate digital robot forum, we can see the "new wisdom of government affairs" in the field of government and enterprises
- 支持向量机(SVC,NuSVC,LinearSVC)
- threejs辉光通道01(UnrealBloomPass && layers)
猜你喜欢

2022.6.13-6.19 AI行业周刊(第102期):职业发展

Spark - LeftOuterJoin 结果条数与左表条数不一致
![[noi Simulation Competition] send (tree DP)](/img/5b/3beb9f5fdad00b6d5dc789e88c6e98.png)
[noi Simulation Competition] send (tree DP)

【bug】@JsonFormat 使用时出现日期少一天的问题

支持向量机(SVC,NuSVC,LinearSVC)

Transplantation of xuantie e906 -- fanwai 0: Construction of xuantie c906 simulation environment

Kaformer personal notes

玄铁E906移植----番外0:玄铁C906仿真环境搭建

华为路由器:GRE技术

Linux MySQL installation
随机推荐
Target detection series fast r-cnn
Framework tool class obtained by chance for self use
leetcode——错误的集合
深入解析 Apache BookKeeper 系列:第三篇——读取原理
【gdb调试工具】| 如何在多线程、多进程以及正在运行的程序下调试
Easyexcel single sheet and multi sheet writing
Get post: do you really know the difference between requests??????
Code written by mysql, data addition, deletion, query and modification, etc
When programmers are asked if they can repair computers... | daily anecdotes
[redis implements seckill business ①] seckill process overview | basic business implementation
支持向量机(SVC,NuSVC,LinearSVC)
Redis implements a globally unique ID
深入了解 border
Data middle office: middle office practice and summary
零基础自学SQL课程 | 相关子查询
Squid代理服务器应用
Implementation process of tcpdump packet capturing
Linux (centos7.9) installation and deployment of MySQL Cluster 7.6
【bug】@JsonFormat 使用时出现日期少一天的问题
CF566E-Restoring Map【bitset】