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

织梦 网站图标飞凡网站建设

织梦 网站图标,飞凡网站建设,网站开发前期工作,杭州自助建站链表内指定区间反转_牛客题霸_牛客网 (nowcoder.com) 思路就是&#xff0c;把需要反转的结点放入栈中&#xff0c;然后在弹出来。 /*** struct ListNode {* int val;* struct ListNode *next;* ListNode(int x) : val(x), next(nullptr) {}* };*/#include<stack> class…

链表内指定区间反转_牛客题霸_牛客网 (nowcoder.com)

思路就是,把需要反转的结点放入栈中,然后在弹出来。

/*** struct ListNode {*	int val;*	struct ListNode *next;*	ListNode(int x) : val(x), next(nullptr) {}* };*/#include<stack>
class Solution {
public:/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类*/ListNode* reverseBetween(ListNode* head, int m, int n) {stack<int>s;int cnt=1;ListNode* pre = head;//前一个结点ListNode* cur = head;//当前节点ListNode* res = head;//如果从头节点开始反转,需要重新弄一个头节点以便返回iListNode* tmp = head;//临时结点ListNode T(0);if (m == 1){while (cnt <= n){s.push(cur->val);cnt++;cur = cur->next;}ListNode*T = new ListNode(s.top());res = T;tmp = res;s.pop();while (!s.empty()){ListNode*T = new ListNode(s.top());tmp->next = T;s.pop();tmp = tmp->next;}while (cur){tmp->next = cur;tmp=cur;cur = cur->next;}return res;}else{while (cnt != m)//这个while世找到m{cnt++;pre = cur;cur = cur->next;}while (cnt <=  n)//入栈{s.push(cur->val);cnt++;cur = cur->next;}//cout<<cur->val<<endl;while (!s.empty())//重新连接{//cout<<s.top()<<endl;ListNode*T = new ListNode(s.top());pre->next = T;s.pop();pre = pre->next;//cout<<pre->val<<endl;}//cout<<pre->val<<endl;while (cur)//连n之后的{pre->next = cur;pre=cur;//cout<<pre->val<<endl;cur = cur->next;}return head;}// write code here}
};

下面有一个错误代码,与上面不一的地方在于

class Solution {
public:ListNode* reverseBetween(ListNode* head, int m, int n) {stack<int>s;int cnt=1;ListNode* pre = head;ListNode* cur= head;ListNode *res=head;ListNode* tmp=head;if (m == 1){while (cnt <= n){s.push(cur->val);cnt++;cur = cur->next;}res = &ListNode(s.top());tmp = res;s.pop();while (!s.empty()){tmp->next = &ListNode(s.top());s.pop();tmp = tmp->next;}while (cur){tmp->next = cur;cur = cur->next;}return res;}else{while (cnt != m){cnt++;pre = cur;cur = cur->next;}while (cnt <= n){s.push(cur->val);cnt++;cur = cur->next;}while (!s.empty()){pre->next = &ListNode(s.top());s.pop();pre = pre->next;}while (cur){tmp->next = cur;cur = cur->next;}return head;}// write code here}
}
/*** struct ListNode {*	int val;*	struct ListNode *next;*	ListNode(int x) : val(x), next(nullptr) {}* };*/#include<stack>
ListNode T(0);
class Solution {
public:/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类*/ListNode* reverseBetween(ListNode* head, int m, int n) {stack<int>s;int cnt=1;ListNode* pre = head;ListNode* cur = head;ListNode* res = head;ListNode* tmp = head;//ListNode T(0);if (m == 1){while (cnt <= n){s.push(cur->val);cnt++;cur = cur->next;}T=ListNode(s.top());res = &T;tmp = res;s.pop();while (!s.empty()){T=ListNode(s.top());tmp->next = &T;s.pop();tmp = tmp->next;}while (cur){tmp->next = cur;cur = cur->next;}return res;}else{while (cnt != m){cnt++;pre = cur;cur = cur->next;}while (cnt <= n){s.push(cur->val);cnt++;cur = cur->next;}//cout<<cur->val<<endl;while (!s.empty()){//cout<<s.top()<<endl;T=ListNode(s.top());pre->next = &T;s.pop();pre = pre->next;//cout<<pre->val<<endl;}//cout<<pre->val<<endl;while (cur){pre->next = cur;pre=cur;//cout<<pre->val<<endl;cur = cur->next;}return head;}// write code here}
};

 

 一个是new一个结点,另一个是建一个结点然后取地址,错误的代码在于,

就是错误代码,我想的是自己创的局部变量T,这个对象每次都是新的,结果在VS2022上调试,一步步发现,原来每次新建的T居然是一个地址!!因为链表里已经加入了之前的T的这个地址,这直接导致链表发生错误,以第一个用例为例,本意是想实现1-4-3-2,结果最后只有1-2

因为在循环时,首先是1-4,在创建3这个节点时,由于是4的地址,所以就把4覆盖了,输出1-3,而不是1-4-3.
链表申请新节点要new。这样每次地址都不一样就连起来了。



下面讲一种,加入一个虚拟节点(哨兵)的做法,之所以这样是可以当从头节点开始转变时,代码不用特判。

 

/*** struct ListNode {*	int val;*	struct ListNode *next;*	ListNode(int x) : val(x), next(nullptr) {}* };*/
class Solution {
public:/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类*/ListNode* reverseBetween(ListNode* head, int m, int n) {ListNode* res=new ListNode(0);//虚拟节点res->next=head;ListNode* pre=res;ListNode* cur=head;ListNode* beg=head;ListNode* en=head;ListNode* tmp;for(int i=1;i<m;i++){pre=beg;beg=beg->next;}for(int i=1;i<n;i++){en=en->next;}cur=beg->next;beg->next=en->next;pre->next=en;pre=beg;while(pre!=en){tmp=cur->next;cur->next=pre;pre=cur;cur=tmp;}return res->next;// write code here}
};

第三个方法

 

/*** struct ListNode {*	int val;*	struct ListNode *next;*	ListNode(int x) : val(x), next(nullptr) {}* };*/
class Solution {
public:/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param head ListNode类 * @param m int整型 * @param n int整型 * @return ListNode类*/ListNode* reverseBetween(ListNode* head, int m, int n) {ListNode* res=new ListNode(0);//虚拟节点res->next=head;ListNode* pre=res;ListNode* cur=head;ListNode* beg=head;ListNode* tmp;for(int i=1;i<m;i++){pre=beg;beg=beg->next;}tmp=beg->next;for(int i=m+1;i<=n;i++){cur=tmp;tmp=tmp->next;beg->next=tmp;cur->next=pre->next;pre->next=cur;}return res->next;// write code here}
};



 


 

 

http://www.yayakq.cn/news/251412/

相关文章:

  • 查询单位信息的网站网站建设招标书
  • 视频网站为什么有人做wordpress 图片集插件
  • 好的网站推广广州新际网站建设
  • 深圳做棋牌网站建设个人兴趣图片集网站建设
  • 制作制作网站建设的ip开源网站fpga可以做点什么用
  • 网站单页面怎么做的网站建设定制公众号小程序
  • 网站开发有什么注意的建设网站需要多少人
  • 网站建设的营业执照广州市场调研公司
  • 天津企业网站建站模板名字logo设计在线生成免费
  • 赣州网站建设中心网页小游戏入口
  • 网站建设及维护费算业务宣传费源码商城源码
  • 网站更改关键词制作app需要哪些知识
  • 网站外链建设设计wordpress mysql版本
  • 网站建设与设计ppt模板下载网站后台构建
  • dedecms手机网站插件整站外包优化公司
  • php网站建设的毕设报告广州家居网站设计
  • 设计做任务的网站怎样创建旅游网站
  • 做网站颜色类型是啥wordpress 编辑器 高亮 引用
  • 做网站,图片显示不出来第一次做网站做什么比较好
  • 网站建设维护管理办法网站设计开发是啥
  • 杭州做服装电商拿货的网站wordpress 提高速度
  • 备案价公示网站企业微信开发公司
  • 珠海网站建设联系方式苏州企业黄页
  • jsp网站 自动发送邮件北京专业网站翻译影音字幕翻译速记速记快而高效
  • 网站如何做反链湖南搜索引擎推广服务
  • 网站开发调查问卷题企业电话号码查询系统
  • 苏州网站开发建设电话网站调用谷歌地图
  • 珠海网站建设创意提交网站收录
  • 网络科技有限公司官网长春百度搜索优化
  • 株洲市哪里有做公司官方网站wordpress标题关键词描述