[LeetCode] Reverse Linked List I II - 链表翻转问题
题目概述:
Reverse a singly linked list.
翻转一个单链表,如:1->2 输出 2->1;1->2->3 输出3->2->1。
题目解析:
本人真的比较笨啊!首先想到的方法就是通过判断链尾是否存在,再新建一个链表,每次移动head的链尾元素,并删除head链表中的元素,一个字“蠢”,但好歹AC且巩固了链表基础知识。你可能遇见的错误包括:
1.'ListNode' undeclared (first use in this function)
nhead=(istNode*)malloc(sizeof(ListNode));
=>
nhead=(struct ListNode*)malloc(sizeof(struct ListNode));
2.Time Limit Exceeded
在链表遍历寻找最后一个结点并插入新链表尾部中需要注意,建议的方法:
q=head; while(q) {q=q->next;}
p=(struct ListNode*)malloc(sizeof(struct ListNode));
p->val=head->val; p->next=NULL; q=p;
=>
q=head; while(q) {last=q; q=q->next;}
p=(struct ListNode*)malloc(sizeof(struct ListNode));
p->val=head->val; p->next=NULL; last->next=p;
通过借助last变量更直观,否则结果总是错误。而且此时q为next指向NULL,如果用到q->next=p就会出现RE错误,因为q都为NULL,哪来的q->next。第二个错误也可能是我个人的编程习惯吧!
第二种方法更为推荐——直接翻转,还有一种递归方法自行提高。
如下图所示,红色表示初始链表存在4个值[1, 2, 3, 4],蓝色表示初始指针first指向第一个元素、second指向第二个元素(head->next),third指向第三个元素;首先s->next=f断开链表并翻转指向第一个元素,同时f=s最后返回first。如果只有两个元素[1, 2]则执行"s->next=f; f=s;"后s=t=NULL返回f即可输出[2, 1]。
我的代码:
直接翻转方法
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/
struct ListNode* reverseList(struct ListNode* head) {struct ListNode *first,*second,*third;if(head==NULL||head->next==NULL)return head;first = head;second = head->next;first->next = NULL;while(second!=NULL) { //注意while(second)不能执行third = second->next;second->next = first;first = second;second = third;}return first;
}
"蠢"方法
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*///个人思路:判断链尾是否存在 翻转到一个新链表
struct ListNode* reverseList(struct ListNode* head) {struct ListNode *nhead,*q,*p,*last,*nq,*np;int value;if(head==NULL||head->next==NULL)return head;q=head;nhead=NULL; //创建新表头while(q->next) {//删除最后一个链尾结点p=q;while(p->next) {last=p;p=p->next; }value=p->val;last->next=NULL;free(p);//插入行结点nq=nhead;while(nq) {last=nq;nq=nq->next;}if(nhead==NULL) { //创建表头np=(struct ListNode*)malloc(sizeof(struct ListNode));np->val=value;nhead=np;nhead->next=NULL;}else { //插入结点np=(struct ListNode*)malloc(sizeof(struct ListNode));np->val=value;np->next=NULL;last->next=np;}//q结点循环前始终指向链表头q=head;}//最后一个结点及头结点headnq=nhead;while(nq) {last=nq; //使用nq=np总是报错WRnq=nq->next;}np=(struct ListNode*)malloc(sizeof(struct ListNode));np->val=head->val;np->next=NULL;last->next=np; //nq->next=np会报错RE 因为nq此时为next及null,而nq->next更不知道在哪return nhead;
}
(By:Eastmount 2015-9-14 晚上7点 http://blog.csdn.net/eastmount/ )
[LeetCode] Reverse Linked List I II - 链表翻转问题相关推荐
- Leetcode 142. Linked List Cycle II
地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...
- 【To Do】LeetCode 142. Linked List Cycle II
LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...
- LeetCode | Reverse Linked List II
这道链表反转题也是搞的我焦头烂额,好久没有写链表了,注意记忆这些 Reverse Linked List II QuestionEditorial Solution My Submissions To ...
- [LeetCode] Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1-> ...
- Leetcode 142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- LeetCode Reverse Linked List II 反置链表2
题意:将指定的一段位置[m,n]的链表反置,返回链表头. 思路:主要麻烦在链表头,如果要从链表头就开始,比较特殊. 目前用DFS实现,先找到m-1的位置,再找到n+1的位置,中间这段就是否要反置的,交 ...
- [LeetCode] [C++] 206 Reverse Linked List 反转单项链表
题目要求 Reverse a singly linked list. LeetCode 206在线测试 问题描述 给定一个单项链表,将其反转后返回链表头节点. 思路分析1 可以完整的遍历一遍链表,将链 ...
- Leetcode: Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass.For example: Given 1-> ...
- leetcode : Reverse Linked List II [two pointers]
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1-> ...
最新文章
- P3879 [TJOI2010]阅读理解 [STL]
- 用国内镜像源pip加速安装模块
- 放话挑战GPT-3!以色列推出参数多30亿、词条多5倍的新语言模型|公测不用排队...
- O/R Mapping 研究报告(转)
- P2730 魔板 Magic Squares
- 指针嵌套指针 拷贝_C++智能指针小结
- 十六、“心念田园穿古镇,足踏古岸望潭汀。”(2021.5.15)
- Spring 基于注解(annotation)的配置之@Autowired注解
- EasyUI---layout布局、树形组件、选项卡tabs
- EMC CTO:IT进化到“以数据为核心”的阶段
- Aspose.Excel模板输出中名称管理器的使用
- WIN7运行命令大全
- Dev-Cpp_6.0下载
- 烽火通信 c语言 笔试,C语言嵌入式笔试题目及参考答案-烽火通信.doc
- 驱动开发:内核遍历进程VAD结构体
- python 微信二次开发_python微信公众账号二次开发
- 组织一个软件开发公司内部培训课程
- 零基础可以学设计专业吗,怎么学?
- 小学生C++编程基础 课程9
- 互联网(internet)
热门文章
- tomcat 相关以及安装时遇到的一些问题整理
- Linux CenOS Python3 和 python2 共存
- Python 文件处理、字符编码(二)
- 洛谷 P1849 [USACO12MAR]拖拉机Tractor
- bzoj 4515: [Sdoi2016]游戏
- 图解CSS3----5-否定伪类选择器
- AtCoder - 2153 An Ordinary Game list模拟 || 博弈
- C语言中的nan和inf使用
- Resharper 5.0 注册码
- execl用宏查询mysql_关于EXCEL能否用宏直接连到oracle数据库然后取数据