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

深圳网络推广公司天涯问答网站标题优化排名

深圳网络推广公司天涯问答,网站标题优化排名,90设计网官网登录,东莞做网站电话目录 💡重排链表 题目描述 方法一: 方法二: 💡旋转链表 题目描述 方法: 💡反转链表|| 题目描述 方法: 💡总结 💡重排链表 题目描述 给定一个单链表 L 的头节…

目录

💡重排链表

题目描述

方法一:

方法二:

💡旋转链表

题目描述

方法:

💡反转链表||

题目描述

方法:

💡总结


💡重排链表

题目描述

给定一个单链表 L 的头节点 head ,单链表 L 表示为:

 L0 → L1 → … → Ln-1 → Ln 
请将其重新排列后变为:

L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …

不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

提示:

  • 链表的长度范围为 [1, 5 * 104]
  • 1 <= node.val <= 1000

方法一:

将链表的每一个节点存在数组里,然后用下标访问的方式,交叉连接。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/typedef struct ListNode ListNode;
void reorderList(struct ListNode* head){if(head->next==NULL||head->next->next==NULL)return;ListNode* arr[50001];ListNode* cur=head;int n=0;while(cur){arr[n]=cur;cur=cur->next;n++;}int i=0;int j=n-1;while(i<j){arr[i]->next=arr[j];i++;arr[j]->next=arr[i];j--;}arr[i]->next=NULL;}

方法二:

可以先用快慢指针的方法找到链表的中间节点,然后将中点后的链表翻转成一个新的链表,最后将这个新链表和原链表切割掉中间节点之后的链表合并成一个新的链表,合并方式是交叉合并。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/typedef struct ListNode ListNode;
ListNode* MiddleNode(ListNode* head)
{ListNode* fast=head;ListNode* slow=head;while(fast!=NULL&&fast->next!=NULL){fast=fast->next->next;slow=slow->next;}return slow;
}
ListNode* ReverseList(ListNode* head)
{ListNode* phead=NULL;ListNode* cur=head;while(cur){ListNode* tmp=cur->next;//注意先后顺序cur->next=phead;phead=cur;cur=tmp;}return phead;
}
void reorderList(struct ListNode* head){ListNode* mid=MiddleNode(head);ListNode* phead=ReverseList(mid->next);mid->next=NULL;ListNode* cur=head;while(phead){ListNode* next=cur->next;cur->next=phead;ListNode* tmp= phead->next;phead->next=next;phead=tmp;cur=cur->next->next;}}

💡旋转链表

题目描述

给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。

提示:

  • 链表中节点的数目在范围 [0, 500] 内
  • -100 <= Node.val <= 100
  • 0 <= k <= 2 * 109

方法:

要求每个节点向右移动k位置,其实就是将倒数k个结点接在头节点之前,倒数第k个结点成为新的头节点,但是这里需要对k进行处理,因为k可能大于链表长度,所以k=k%len,还有一个需要注意的就是当k==len时,是不需要进行任何操作的,直接返回头节点就可以了。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
typedef struct ListNode ListNode;
struct ListNode* rotateRight(struct ListNode* head, int k) {if(head==NULL||k==0)return head;ListNode* cur=head;ListNode* prev=head;ListNode* ret=head;int l=0;while(ret){ret=ret->next;l++;}k=k%l;if(k==0)return head;while(k--){cur=cur->next;}while(cur->next){cur=cur->next;prev=prev->next;}ListNode*  Next=prev->next;cur->next=head;prev->next=NULL;return Next;
}

💡反转链表||

题目描述

给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。

提示:

  • 链表中节点数目为 n
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n

方法:

我的方法就是将区间[left,right]之间的结点翻转,然后与原来区间前后的结点重新连接起来。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
typedef struct ListNode ListNode;
struct ListNode* reverseList(struct ListNode* head,struct ListNode* tail)
{ListNode*phead=NULL;//新的头ListNode*cur=head;while(cur!=tail)//遍历原链表{ListNode*next=cur->next;//保存下一个节点的地址,避免丢失cur->next=phead;phead=cur;//更新头节点cur=next;//继续遍历}return phead;
}
struct ListNode* reverseBetween(struct ListNode* head, int left, int right) {ListNode* cur1=head;ListNode* cur2=head;int cnt=1;while(cnt<left-1){cur1=cur1->next;cur2=cur2->next;cnt++;}while(cnt<=right){cur2=cur2->next;cnt++;}ListNode* tail=NULL;               if(cur2!=NULL)tail=cur2;if(left==1)head=reverseList(cur1,cur2);elsecur1->next=reverseList(cur1->next,cur2);while(cur1->next){cur1=cur1->next;}cur1->next=tail;return head;
}

💡总结

链表相关的题目还是要注意细节,结点之间的切割与连接要特别仔细,不然任意造成空结点,或者成环导致死循环。

http://www.ritt.cn/news/22977.html

相关文章:

  • 苏州园区两学一做网站北京网站排名推广
  • 网站经营跟备案不符外贸营销型网站制作公司
  • 中文网站建设哪家好企业网站建设规划
  • 网络营销建设seo求职信息
  • 苏州企业网站建站系统网站推广的途径和方法
  • 网站代理登录网址免费站长工具
  • 青岛开发区网站建设公司宁波网络推广
  • 上海门户网站怎么登录建网站怎么赚钱
  • 网站模版怎么做的网站建设优化400报价
  • wordpress调用留言板安徽seo
  • 哈尔滨网站建设公司百度后台推广登录
  • 网站怎么做动静分离网站权重划分
  • 国内精美网站欣赏什么时候友情链接
  • 中药材天地网做中药零售网站亚马逊关键词排名查询工具
  • 网络公司给别人做网站的cms是买的授权么免费建站的网站哪个好
  • 单位 内网网站建设苏州百度搜索排名优化
  • 优秀网站模板下载电商网络营销
  • 做海报找素材网站衡阳网站建设
  • 网站建设开发价格高吗正规电商平台有哪些
  • 昆明设计网站建设宁波关键词网站排名
  • 购物网站开发需要什么软件青岛seo关键词
  • 黄岛网站建设公司哪家好百度网站统计
  • 网站公安备案制度搜索引擎优化工具有哪些
  • 彩票网站开发. 极云营销技巧在线完整免费观看
  • 第三方客服系统平台网站优化排名网站
  • 电脑编程多合一seo插件破解版
  • 网站设计都用什么字体优秀软文范例800字
  • 哪个网站能上传自己做的简历域名服务器地址查询
  • 网站制作软件下载安装整站优化和单词
  • wordpress制作百度地图xmlseo课程培训入门