206. Reverse Linked List&&92. Reverse Linked List II
2016-05-17 14:53
288 查看
依然不想赘述题目……
206一开始想了有点久,因为网上很多答案是没有 * 的,就像这样 ListNode pre = head
宝宝就有点没拐过弯来。改天要复习一下*和&。
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head == NULL || head->next == NULL) return head;
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* pre = dummy;
ListNode* cur = head;
while(cur->next != NULL ){
ListNode* nxt = cur->next;
cur->next = nxt->next;
nxt->next = pre->next;
pre->next = nxt;
}
return dummy->next;
}
};
207也写了很久……WA无数次……是啦我就是渣渣
206一开始想了有点久,因为网上很多答案是没有 * 的,就像这样 ListNode pre = head
宝宝就有点没拐过弯来。改天要复习一下*和&。
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head == NULL || head->next == NULL) return head;
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* pre = dummy;
ListNode* cur = head;
while(cur->next != NULL ){
ListNode* nxt = cur->next;
cur->next = nxt->next;
nxt->next = pre->next;
pre->next = nxt;
}
return dummy->next;
}
};
207也写了很久……WA无数次……是啦我就是渣渣
class Solution { public: ListNode* reverseBetween(ListNode* head, int m, int n) { if(head == NULL) return NULL; ListNode* dummy = new ListNode(0); dummy->next = head; ListNode* pre = dummy; for(int i = 1 ; i < m ; ++i){ pre = pre->next; } ListNode* cur = pre->next; for(int i = m; i < n; ++ i){ ListNode* nxt = cur->next; cur->next = nxt->next; nxt->next = pre->next; pre->next = nxt; } return dummy->next; } };
相关文章推荐
- 如何从按行读文件&&以及每行中有多少个数据块,并输出每个数据块
- 欢迎使用CSDN-markdown编辑器
- Android让AlertDialog点击确定不会消失
- 把数组排成最小的数
- Android省市区三级联动滚轮选择(真实项目中提取出来的组件)
- 百度地图Poi关键字查询附近地点
- 寻找数组中第K频繁的元素
- jquery 双击修改某项值
- java 集合框架总结
- ThreadLocal源码分析
- 一文搞懂算法的时间复杂度与空间复杂度
- 以C程序为例内存分布详解
- Pycharm漫天波浪线问题
- Cloudera CDH 、Impala本地通过Parcel安装配置详解及什么是Parcel
- 水题 约瑟夫问题 STL
- Struts2中的国际化
- 大整数类运算 BigInteger
- Android 自定义侧滑菜单
- Android省市区三级联动滚轮选择(真实项目中提取出来的组件)
- 坑爹的ndk-build