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

物流公司网站建设模板文章代写

物流公司网站建设模板,文章代写,中国建设银行官网站电脑版,电商网站设计思路本篇文章主要是对力扣和牛客网上一些经典的和链表有关的笔试题的总结归纳,希望对你有所帮助。 目录 一、移除链表元素 1.1 问题描述 1.2 思路一 1.2.1 分析 1.2.2 代码 1.3 思路二 1.3.1 分析 1.2.3 思路三 1.3 代码实现 1.3.1 思路1的代码 1.3.2 思路2的…

     本篇文章主要是对力扣和牛客网上一些经典的和链表有关的笔试题的总结归纳,希望对你有所帮助。

目录

一、移除链表元素

1.1 问题描述

1.2 思路一

1.2.1 分析

1.2.2 代码

1.3 思路二

1.3.1 分析

1.2.3 思路三

1.3 代码实现

1.3.1 思路1的代码

1.3.2 思路2的代码

二、链表的中间结点

2.1 问题描述

2.2 思路一

2.2.1 分析

 2.2.2 代码

2.3 思路二

2.3.1 分析

 2.3.2 代码

三、链表中倒数第k个结点

3.1 问题描述

3.2 思路一

3.2.1 分析

3.2.2 思路

3.3 思路二

3.3.1 分析

3.3.2 代码

四、反转链表

4.1 问题描述

4.2 思路一

4.2.1 分析

4.2.2 代码

4.3 思路二

4.3.1 分析

4.3.2 代码

五、合并两个有序链表

5.1 问题描述

5.2 思路一

5.2.1 分析

5.2.2 代码

5.3 思路二

5.3.1 分析

5.3.2 代码

六、链表分割

6.1 问题描述

6.2 思路

6.2.1 分析

6.2.2 代码

七、链表的回文结构

7.1 问题描述

7.2 思路

7.2.1 分析

7.2.2 代码

八、相交链表

8.1 问题描述

8.2 思路

8.2.1 分析

8.2.2 代码


一、移除链表元素

1.1 问题描述

删除链表中等于给定值 val 的所有结点。
原题链接:https://leetcode.cn/problems/remove-linked-list-elements/description/

1.2 思路一

1.2.1 分析

     双指针的方式,cur中存储的数据如果等于val,就让cur的前一个结点prev的指针指向cur的后一个结点,然后把cur的空间释放了,再继续向后遍历。

在这种情况下我们需要考虑头结点的值就等于val的情况即下例:

1.2.2 代码

struct ListNode* removeElements(struct ListNode* head, int val){struct ListNode* cur = head;struct ListNode* prev = NULL;while(cur){if(head->val==val){head = head->next;free(cur);cur =head;}else {if(cur->val==val){prev->next = cur->next;free(cur);cur = prev->next;}else{prev = cur;cur = cur->next;}}}return head;
}

1.3 代码2

struct ListNode* removeElements(struct ListNode* head, int val){if(head==NULL){return NULL;}struct ListNode* cur = head;struct ListNode* newhead,*tail;newhead = tail = NULL;while(cur){if(cur->val!=val){if(tail==NULL){newhead = tail = cur;}else {tail->next = cur;tail = cur;}cur = cur->next;}else {struct ListNode* next = cur->next;free(cur);cur = next;}}if(tail){tail->next =NULL;}return newhead;}

1.4 代码3

struct ListNode* removeElements(struct ListNode* head, int val){if(head==NULL){return NULL;}struct ListNode* guard,*tail;guard = tail = (struct ListNode*)malloc(sizeof(struct ListNode));guard->next = NULL;struct ListNode* cur = head;while(cur){if(cur->val!=val){tail->next = cur;tail = cur;cur = cur->next;}else {struct ListNode* next = cur->next;free(cur);cur = next;}}tail->next = NULL;struct ListNode* p = guard->next;free(guard);return p;}

二、链表的中间结点

2.1 问题描述

oj链接:876. 链表的中间结点 - 力扣(LeetCode)

2.2 思路一

2.2.1 分析

     我们可以使用快慢指针,快慢指针都从head即头结点开始,慢指针slow每次走一步,快指针fast每次走两步,对于此题链表中结点的个数为偶数个和为奇数个时的结束条件不同。

     如果链表的结点个数是奇数个,那么当快指针走到最后一个结点时,慢指针正好到达中间结点。

      如果链表的结点个数是偶数个,那么就有两个中间结点,题中说明如果有两个中间结点,返回第二个中间结点。当快指针走到最后一个结点的下一个即走到空时,慢指针正好到达中间结点。

 2.2.2 代码

struct ListNode* middleNode(struct ListNode* head){struct ListNode* slow = head,*fast = head;while(fast&&fast->next){slow = slow->next;fast = fast->next->next;}return slow;
}

2.3 思路二

2.3.1 分析

     通过遍历求出链表结点的总个数,然后得到中间结点的位置,再次遍历找到中间结点,返回。

 2.3.2 代码

struct ListNode* middleNode(struct ListNode* head){struct ListNode* cur = head;int count = 0;while(cur){count++;cur = cur->next;}int middle = count/2;cur = head;while(middle--){cur = cur->next;} return cur;
}

三、链表中倒数第k个结点

3.1 问题描述

牛客链接:链表中倒数第k个结点_牛客题霸_牛客网 (nowcoder.com)

3.2 思路一

3.2.1 分析

     使用快慢指针,慢指针slow和快指针fast之间距离相差k,然后同步移动,当快指针fast移动到空的时候,慢指针指向的就是倒数第k个结点。

3.2.2 思路

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {struct ListNode* fast = pListHead;struct ListNode* slow = pListHead;while(k--){if(fast!=NULL)fast = fast->next;elsereturn NULL;}while(fast){slow = slow->next;fast = fast->next;}return slow;}

3.3 思路二

3.3.1 分析

     求出整个链表的结点个数,倒数第k个结点就是从头结点向后走n-k次。

3.3.2 代码

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {int count = 0;struct ListNode* cur = pListHead;while(cur){count++;cur = cur->next;}if(k>count){return NULL;}int x = count - k;cur = pListHead;while(x--){cur = cur->next;}return cur;}

四、反转链表

4.1 问题描述

oj链接:206. 反转链表 - 力扣(LeetCode)

4.2 思路一

4.2.1 分析

     取链表中的结点头插到一个新链表上,然后返回新链表的头结点。

4.2.2 代码

struct ListNode* reverseList(struct ListNode* head){struct ListNode* cur = head;struct ListNode* newhead = NULL;struct ListNode* next = NULL;while(cur){next = cur->next;cur->next = newhead;newhead = cur;cur = next;}return newhead;
}

4.3 思路二

4.3.1 分析

     把链表的指针翻转,让每一个指针指向他的前一个。

4.3.2 代码

struct ListNode* reverseList(struct ListNode* head){if(head==NULL)return NULL;struct ListNode* cur = head,*prev = NULL;struct ListNode* next = cur->next;while(cur){next = cur->next;cur->next = prev;prev = cur;cur=next;}return prev;}

五、合并两个有序链表

5.1 问题描述

oj链接:21. 合并两个有序链表 - 力扣(LeetCode)

5.2 思路一

5.2.1 分析

     依次比较两个链表的结点,取出小的结点尾插到一个新链表上,当有一个链表走到空时,直接将剩下的那个链表链接到新链表上。

在此方法中我们需要注意:

  1. 需要单独考虑到两个链表中有空链表的情况。
  2. 在尾插时,考虑新链表为空的情况,需要单独处理。

5.2.2 代码

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){struct ListNode* n1 = list1,*n2 = list2;struct ListNode* newhead = NULL,*tail = NULL;if(n1==NULL){return n2;}if(n2==NULL){return n1;}while(n1&&n2){if(n1->val<n2->val){if(newhead == NULL){newhead = tail = n1; }else{tail->next = n1;tail = tail->next;}n1 = n1->next;;}else{if(newhead == NULL){newhead = tail = n2; }else{tail->next = n2;tail = tail->next;}n2 = n2->next;;}}if(n1){tail->next = n1;}if(n2){tail->next = n2;}return newhead;}

5.3 思路二

5.3.1 分析

     思路二其实是思路一的改进,思路二中引用了带哨兵位的头结点的链表,这样就不需要再单独处理尾插时新链表为空和两个链表中有链表为空的问题,在这里创建哨兵位的头结点主要用动态开辟的方式,当不再使用时需要释放。

5.3.2 代码

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){struct ListNode* n1 = list1,*n2 = list2;struct ListNode* newhead = (struct ListNode*)malloc(sizeof(struct ListNode));newhead->next = NULL;struct ListNode* tail = newhead;while(n1&&n2){if(n1->val>n2->val){tail->next = n2;n2 = n2->next;tail = tail->next;}else{tail->next = n1;n1 = n1->next;tail=tail->next;}}if(n1){tail->next = n1;}if(n2){tail->next = n2;}struct ListNode* next = newhead->next;free(newhead);return next;}

六、链表分割

6.1 问题描述

oj链接:链表分割_牛客题霸_牛客网 (nowcoder.com)

6.2 思路

6.2.1 分析

     新建两个链表,把小于x的尾插到其中一个链表,大于等于x的尾插到另一个链表,然后把这两个链表链接起来。注意我们新建的两个链表最好用带哨兵位的头结点的链表,这样可以避免我们在尾插时需要对空单独分析的问题。

6.2.2 代码

class Partition {
public:ListNode* partition(ListNode* pHead, int x) {// write code herestruct ListNode* lesshead = NULL,*lesstail= NULL;struct ListNode* greaterhead = NULL,*greatertail= NULL;struct ListNode* cur = pHead;lesshead = lesstail = (struct ListNode*)malloc(sizeof(struct ListNode));greaterhead = greatertail= (struct ListNode*)malloc(sizeof(struct ListNode));while(cur){if(cur->val<x){lesstail->next = cur;lesstail=lesstail->next;}else {greatertail->next = cur;greatertail=greatertail->next;}cur = cur->next;}greatertail->next=NULL;lesstail->next = greaterhead->next;free(greaterhead);struct ListNode* p = lesshead->next;free(lesshead);return p;}
};

七、链表的回文结构

7.1 问题描述

oj链接:链表的回文结构_牛客题霸_牛客网 (nowcoder.com)

7.2 思路

7.2.1 分析

     在这里我们提供一个思路,首先找到中间结点,然后将中间结点(包括中间结点)后面的部分逆置,然后将前半段和后半段进行比较。

     在之前的题目中,我们已经写过了找中间结点以及链表反转的代码,在这里直接拷贝。

7.2.2 代码

struct ListNode* middleNode(struct ListNode* head){struct ListNode* slow = head,*fast = head;while(fast&&fast->next){slow = slow->next;fast = fast->next->next;}return slow;
}struct ListNode* reverseList(struct ListNode* head){if(head==NULL)return NULL;struct ListNode* cur = head,*prev = NULL;struct ListNode* next = cur->next;while(cur){next = cur->next;cur->next = prev;prev = cur;cur=next;}return prev;}class PalindromeList {public:bool chkPalindrome(ListNode* A) {struct ListNode* mid =  middleNode(A);struct ListNode* n2 = reverseList(mid);struct ListNode* n1 = A;while(n2&&n1){if(n1->val!=n2->val){return false;}n2 = n2->next;n1 = n1->next;}return true;
}
};

八、相交链表

8.1 问题描述

oj链接:160. 相交链表 - 力扣(LeetCode)

8.2 思路

8.2.1 分析

     分别求两个链表的长度,长的链表先走差距步,然后再同时走,第一个地址相同的指针就是交点。

8.2.2 代码

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {struct ListNode* n1 = headA;struct ListNode* n2 = headB;int x1 = 0;int x2 = 0;while(n1){x1++;n1 = n1->next;}while(n2){x2++;n2 = n2->next;}if(n1!=n2)return NULL;int x = abs(x1-x2);struct ListNode * shortList = headA,*longList = headB;if(x1>x2){shortList = headB;longList = headA;}while(x--){longList = longList->next;}while(longList!=shortList){longList = longList->next;shortList = shortList->next;}return longList;}


文章转载自:
http://wanjiauintathere.qnzk.cn
http://wanjiainconsequentia.qnzk.cn
http://wanjiacicely.qnzk.cn
http://wanjiareprofile.qnzk.cn
http://wanjiafreesia.qnzk.cn
http://wanjiableacher.qnzk.cn
http://wanjiaprimula.qnzk.cn
http://wanjiamarsupium.qnzk.cn
http://wanjiacymbal.qnzk.cn
http://wanjiacarbazole.qnzk.cn
http://wanjiadewdrop.qnzk.cn
http://wanjiastuffiness.qnzk.cn
http://wanjiariempie.qnzk.cn
http://wanjiaeremophilous.qnzk.cn
http://wanjiaphylogenic.qnzk.cn
http://wanjiapassword.qnzk.cn
http://wanjiahokum.qnzk.cn
http://wanjialatter.qnzk.cn
http://wanjiaastrodynamics.qnzk.cn
http://wanjiaisland.qnzk.cn
http://wanjiaadjudgement.qnzk.cn
http://wanjiabarrator.qnzk.cn
http://wanjiahypethral.qnzk.cn
http://wanjiacankered.qnzk.cn
http://wanjiachaetognath.qnzk.cn
http://wanjiamaisie.qnzk.cn
http://wanjiaveterinarian.qnzk.cn
http://wanjiahairweaving.qnzk.cn
http://wanjiainstilment.qnzk.cn
http://wanjiadenial.qnzk.cn
http://wanjiaschistocyte.qnzk.cn
http://wanjiabackcross.qnzk.cn
http://wanjiajeopard.qnzk.cn
http://wanjianavajo.qnzk.cn
http://wanjiadermestid.qnzk.cn
http://wanjiaossify.qnzk.cn
http://wanjiaglacier.qnzk.cn
http://wanjiaepiandrosterone.qnzk.cn
http://wanjiacragged.qnzk.cn
http://wanjiasleet.qnzk.cn
http://wanjiahafiz.qnzk.cn
http://wanjiainset.qnzk.cn
http://wanjiaradioisotope.qnzk.cn
http://wanjiaphotoreconnaissance.qnzk.cn
http://wanjiadacryocystorhinostomy.qnzk.cn
http://wanjiaheliogabalus.qnzk.cn
http://wanjiafirry.qnzk.cn
http://wanjiacirculate.qnzk.cn
http://wanjiauglification.qnzk.cn
http://wanjiawording.qnzk.cn
http://wanjiaobit.qnzk.cn
http://wanjiaunrealist.qnzk.cn
http://wanjiaoverhit.qnzk.cn
http://wanjiaamused.qnzk.cn
http://wanjiainstinctual.qnzk.cn
http://wanjiaincohesion.qnzk.cn
http://wanjiagynobase.qnzk.cn
http://wanjiadivarication.qnzk.cn
http://wanjiapredispose.qnzk.cn
http://wanjiaglutei.qnzk.cn
http://wanjiasinogram.qnzk.cn
http://wanjiatawdrily.qnzk.cn
http://wanjiaenfeeble.qnzk.cn
http://wanjiafestival.qnzk.cn
http://wanjiaambisonics.qnzk.cn
http://wanjiafashion.qnzk.cn
http://wanjiahertfordshire.qnzk.cn
http://wanjiauntrusty.qnzk.cn
http://wanjiacommandeer.qnzk.cn
http://wanjiaschvartze.qnzk.cn
http://wanjiacur.qnzk.cn
http://wanjiacultured.qnzk.cn
http://wanjiahousecleaning.qnzk.cn
http://wanjiajournalise.qnzk.cn
http://wanjiamomentous.qnzk.cn
http://wanjiacolonic.qnzk.cn
http://wanjiaominous.qnzk.cn
http://wanjiacausalgic.qnzk.cn
http://wanjiacicatrix.qnzk.cn
http://wanjiacoherence.qnzk.cn
http://www.15wanjia.com/news/118559.html

相关文章:

  • 汉阳网站建设互联网项目推广平台有哪些
  • 涞水住房和城乡建设厅网站aso推广优化
  • 免费网站建设力荐 186一6159一6345绘政正规seo关键词优化外包
  • 做微商能利用的网站有哪些百度快速查询
  • 论坛网站制作教程安卓优化大师官方版本下载
  • 做私服网站要多大空间十大电商代运营公司
  • 临沂做企业网站的公司seo快速排名软件网站
  • wordpress设置2个网站吗外贸推广平台哪个好
  • 做盗版网站引流查找关键词的工具叫什么
  • 可信网站服务外贸独立站怎么做
  • wap网站asp源码今天的新闻有哪些
  • 为什么网站需要备案青岛网站制作推广
  • 做变形字的网站惠州网络推广
  • 免费下载应用软件seo网页推广
  • 建各公司网站要多少钱网站关键词排名查询
  • 提供微商城网站建设seo公司彼亿营销
  • 网站结构怎么做app拉新推广平台渠道商
  • 阿里云用什么系统做网站好常用的网络营销推广方法有哪些
  • 做网站实时数据用接口北京seo公司助力网络营销
  • 曲阜建设局网站网页广告调词平台多少钱
  • 长治网站制作报价创建网站要钱吗
  • 河南网站推广优化公司网站宣传推广文案
  • 日本做外贸网站seo网站推广服务
  • 网站建设 申请优化大师
  • 冠县做网站哪里好seo专员的工作内容
  • 西安哪有做网站的百度保障客服电话
  • 做网站用vs还是dw小程序开发费用一览表
  • 代卖平台哪个好谷歌优化师
  • 免费网站申请域名com哪里有正规的电商培训班
  • 深圳网站建设大公司泰州网站优化公司