博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode - Reverse Linked List II
阅读量:5093 次
发布时间:2019-06-13

本文共 3696 字,大约阅读时间需要 12 分钟。

2013.12.31 16:00

Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:

Given 1->2->3->4->5->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:

Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

Solution1:

  Reversing a linked list is typical for a technical interview, but this variation is a bit more diffcult.

  My solution, is to cut the list into three parts: [1, m - 1], [m, n], [n + 1, end], locate the middle part, reverse it and restore the pointers at last.

  Time complexity is O(n), space compexity is O(1).

Accepted code:

1 // 3RE, 1WA, 1AC, so difficult... 2 /** 3  * Definition for singly-linked list. 4  * struct ListNode { 5  *     int val; 6  *     ListNode *next; 7  *     ListNode(int x) : val(x), next(NULL) {} 8  * }; 9  */10 class Solution {11 public:12     ListNode *reverseBetween(ListNode *head, int m, int n) {13         // IMPORTANT: Please reset any member data you declared, as14         // the same Solution instance will be reused for each test case.15         int i;16         ListNode *p1, *p2;17         ListNode *root, *t1, *t2;18         ListNode *par;19         20         root = new ListNode(0);21         root->next = head;22         p1 = root;23         for(i = 0; i < m - 1; ++i){24             p1 = p1->next;25         }26         // 1RE here, wrong pointer27         par = p1;28         p1 = p1->next;29         30         t1 = nullptr;31         t2 = p1;32         // 1RE here, i < n - m + 133         for(i = 0; i < n - m + 1; ++i){34             p2 = t2;35             p2 = p2->next;36             t2->next = t1;37             t1 = t2;38             // 1RE here, wrong pointer39             t2 = p2;40         }41         // 1WA, wrong pointer42         par->next = t1;43         p1->next = t2;44         head = root->next;45         46         delete root;47         return head;48     }49 };

Solution2:

  In the solution above, one new operation was called. This overhead can be reduced with some extra code.

  Always remember that new and delete are expensive operations, especially when they're called unnecessarily or irregularly.

  Please see the code below. Time complexity is O(n), space complexity is O(1).

Accepted code:

1 // 1WA, 1AC, faulty operator is extremely difficult to debug, take this lesson! 2 /** 3  * Definition for singly-linked list. 4  * struct ListNode { 5  *     int val; 6  *     ListNode *next; 7  *     ListNode(int x) : val(x), next(NULL) {} 8  * }; 9  */10 class Solution {11 public:12     ListNode *reverseBetween(ListNode *head, int m, int n) {13         // IMPORTANT: Please reset any member data you declared, as14         // the same Solution instance will be reused for each test case.15         int i;16         ListNode *p1, *p2;17         ListNode *t1, *t2;18         ListNode *par;19         20         // 1WA here, (O_o), != or ==? ...21         if(head == nullptr){22             return head;23         }24         25         if(m > 1){26             p1 = head;27             for(i = 0; i < m - 2; ++i){28                 p1 = p1->next;29             }30             par = p1;31             p1 = p1->next;32         }else{33             par = nullptr;34             p1 = head;35         }36         37         t1 = nullptr;38         t2 = p1;39         for(i = 0; i < n - m + 1; ++i){40             p2 = t2->next;41             t2->next = t1;42             t1 = t2;43             t2 = p2;44         }45         if(par != nullptr){46             par->next = t1;47         }else{48             head = t1;49         }50         p1->next = t2;51         52         return head;53     }54 };

 

转载于:https://www.cnblogs.com/zhuli19901106/p/3499683.html

你可能感兴趣的文章
大数据学习系列(8)-- WordCount+Block+Split+Shuffle+Map+Reduce技术详解
查看>>
Mysql性能调优
查看>>
getElement的几中属性介绍
查看>>
设计器 和后台代码的转换 快捷键
查看>>
STL容器之vector
查看>>
数据中心虚拟化技术
查看>>
复习文件操作
查看>>
SQL Server 使用作业设置定时任务之一(转载)
查看>>
第二阶段冲刺-01
查看>>
BZOJ1045 HAOI2008 糖果传递
查看>>
JavaScript 克隆数组
查看>>
eggs
查看>>
python3 生成器与迭代器
查看>>
《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇03:暂停游戏》
查看>>
CPU,寄存器,一缓二缓.... RAM ROM 外部存储器等简介
查看>>
git .gitignore 文件不起作用
查看>>
Alan Turing的纪录片观后感
查看>>
IOS--沙盒机制
查看>>
sqlite的坑
查看>>
digitalocean --- How To Install Apache Tomcat 8 on Ubuntu 16.04
查看>>