alexa的网站排名主要分为哪两种国外做自动化网站
- 作者: 五速梦信息网
- 时间: 2026年04月16日 21:00
当前位置: 首页 > news >正文
alexa的网站排名主要分为哪两种,国外做自动化网站,重庆做网站开发的公司有哪些,响应式电商网站制作今日学习目标
一、基础 链表
接下来说一说链表的定义。
链表节点的定义#xff0c;很多同学在面试的时候都写不好。
这是因为平时在刷leetcode的时候#xff0c;链表的节点都默认定义好了#xff0c;直接用就行了#xff0c;所以同学们都没有注意到链表的节点是如何定…今日学习目标
一、基础 链表
接下来说一说链表的定义。
链表节点的定义很多同学在面试的时候都写不好。
这是因为平时在刷leetcode的时候链表的节点都默认定义好了直接用就行了所以同学们都没有注意到链表的节点是如何定义的。
而在面试的时候一旦要自己手写链表就写的错漏百出。
这里我给出C/C的定义链表节点方式如下所示
// 单链表
struct ListNode {int val; // 节点上存储的元素ListNode next; // 指向下一个节点的指针ListNode(int x) : val(x), next(NULL) {} // 节点的构造函数
}; 有同学说了我不定义构造函数行不行答案是可以的C默认生成一个构造函数。
但是这个构造函数不会初始化任何成员变量下面我来举两个例子
通过自己定义构造函数初始化节点
ListNode head new ListNode(5); 使用默认构造函数初始化节点
ListNode head new ListNode();
head-val 5;
所以如果不定义构造函数使用默认构造函数的话在初始化的时候就不能直接给变量赋值
二、算法 1. 203. 移除链表元素
/** Definition for singly-linked list.* struct ListNode {* int val;* ListNode next; ListNode() : val(0), next(nullptr) {}* ListNode(int x) : val(x), next(nullptr) {}* ListNode(int x, ListNode next) : val(x), next(next) {} };/
class Solution {
public:ListNode removeElements(ListNode* head, int val) {while (head ! NULL head-val val) {ListNode* p head;head head-next;delete p;}ListNode* cur head;while (cur ! NULL cur-next ! NULL) {if (cur-next-val val) {ListNode* p cur-next;cur-next cur-next-next;delete p;} else {cur cur-next;}}return head;}
};
- 707.设计链表 #include bits/stdc.h using namespace std;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大于链表的长度则返回空// 如果index小于0则在头部插入节点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;}// 删除第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;//delete命令指示释放了tmp指针原本所指的那部分内存//被delete后的指针tmp的值地址并非就是NULL而是随机值。也就是被delete后//如果不再加上一句tmpnullptr,tmp会成为乱指的野指针//如果之后的程序不小心使用了tmp会指向难以预想的内存空间tmpnullptr;_size–;}// 打印链表void printLinkedList() {LinkedNode* cur _dummyHead;while (cur-next ! nullptr) {cout cur-next-val ;cur cur-next;}cout endl;} private:int _size;LinkedNode* _dummyHead;};3. 206.反转链表 class Solution { public:ListNode* reverseList(ListNode* head) {if (head NULL)return head;ListNode* dummyHead new ListNode(0); // 设置一个虚拟头结点dummyHead-next head; // 将虚拟头结点指向head这样方便后面做删除操作ListNode* p dummyHead-next;ListNode* q;dummyHead-next NULL;while (p ! NULL) {q p;p p-next;q-next dummyHead-next;dummyHead-next q;}return dummyHead-next;} }; 4. 24. 两两交换链表中的节点 #include iostream #include bits/stdc.h struct ListNode {int val;ListNode *next;ListNode() : val(0), next(nullptr) {}ListNode(int x) : val(x), next(nullptr) {}ListNode(int x, ListNode next) : val(x), next(next) {} }; int main(){return 0; } ListNode swapPairs(ListNode* head) {if (headNULL || head-nextNULL) {return head;}ListNode *dummyHead new ListNode(0);dummyHead-next head;ListNode *cur dummyHead;ListNode *p,*q;while (cur-next!nullptr cur-next-next!nullptr) {p cur-next;q cur-next-next-next;cur-next p-next;cur-next-next p;cur-next-next-next q;cur cur-next-next;}return dummyHead-next; }5. 19.删除链表的倒数第N个节点 #include bits/stdc.h struct ListNode {int val;ListNode *next;ListNode() : val(0), next(nullptr) {}ListNode(int x) : val(x), next(nullptr) {}ListNode(int x, ListNode next) : val(x), next(next) {} }; int main(){return 0; } ListNode removeNthFromEnd(ListNode* head, int n) {if(headNULL) return NULL;ListNode *dummyNode new ListNode();dummyNode-next head;ListNode *p,*q;pqdummyNode;while (n–p!nullptr) {pp-next;}pp-next;while(p!nullptrq!nullptr){pp-next;qq-next;}ListNode *temp q-next;q-next q-next-next;delete temp;return dummyNode-next; }6. 面试题 02.07. 链表相交 #include bits/stdc.h struct ListNode {int val;ListNode *next;ListNode() : val(0), next(nullptr) {}ListNode(int x) : val(x), next(nullptr) {}ListNode(int x, ListNode *next) : val(x), next(next) {} }; int main(){return 0; } ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {int aLen 0;int bLen 0;ListNode *pa headA;ListNode *pb headB;while(pa!nullptr){aLen;pa pa-next;}while(pb!nullptr){bLen;pb pb-next;}int k;pa headA;pb headB;if(aLenbLen){k bLen - aLen;while (k– pb!nullptr) {pb pb-next;}while(pb!nullptr){if(papb){return pb;}pa pa-next;pb pb-next;}}else{k aLen - bLen;while (k– pa!nullptr) {pa pa-next;}while(pa!nullptr){if(papb){return pa;}pa pa-next;pb pb-next;}}return NULL; }
相关文章
-
ajax+jsp网站开发从入门到精通网页设计培训学校哪家好
ajax+jsp网站开发从入门到精通网页设计培训学校哪家好
- 技术栈
- 2026年04月16日
-
ai可以用来做网站吗怎么制作网站发布
ai可以用来做网站吗怎么制作网站发布
- 技术栈
- 2026年04月16日
-
ae有么有做gif的网站用模板做网站教程
ae有么有做gif的网站用模板做网站教程
- 技术栈
- 2026年04月16日
-
amh面板安装wordpress宁波seo服务快速推广
amh面板安装wordpress宁波seo服务快速推广
- 技术栈
- 2026年04月16日
-
anaconda可以做网站吗jquery网站开发实例
anaconda可以做网站吗jquery网站开发实例
- 技术栈
- 2026年04月16日
-
androidstudio入门教程国家二十条优化措施
androidstudio入门教程国家二十条优化措施
- 技术栈
- 2026年04月16日
