Leetcode解题-链表(2.2.2)ReverseLinkedList

题目:2.2.2 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->2->3->4->5->nullptr, m = 2 and n = 4,

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

Note: Given m, n satisfy the following condition: 1 <= m <= n <= length of list.

链表旋转是个经典问题,可以锻炼我们操作链表的能力,值得好好学习一下。

我的方法:对head和tail特殊化处理

当m<=i<=n时,通过记录前继结点prev,简单的调转next指针方向,并记录m结点pm。最后迭代到n+1时,再处理first(pm->next)和last(p)结点的指向问题。

更好的方法:一般化处理

将整个过程一般化处理的方法为:不断地将当前结点cur插入到head的后面。(见下面图示)第一次将3插入到1后面,第二次将4插入到1后面,即1=>2=>3=>4旋转为1=>3=>2=>4。如果能解决的话,那么继续1=>3=>2=>4=>5就能旋转为1=>4=>3=>2=>5,从而解决问题。总结:首先举最简单的例子解决,然后看解决办法能否继续推广到更复杂一些的例子,避免复杂的特殊化处理

代码实现

理解了整体设计,自己实现或读代码就很简单了。开始真正处理前要记住head(m的前一个结点)和pm(索引为m的结点),就可以开始旋转了。

时间: 2024-10-19 07:04:09

Leetcode解题-链表(2.2.2)ReverseLinkedList的相关文章

Leetcode解题-链表(2.2.1)AddTwoNumbers

1 题目:2.2.1 Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4

Leetcode解题-链表(2.2.3)PartitionList

题目:2.2.3 Partition List Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For exam

Leetcode解题-链表(2.2.6)RotateList

1 题目:Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->nullptr and k = 2, return 4->5->1->2->3->nullptr. 2 实现 首先确定题目要求的旋转都需要哪些结点:dummy头结点.结点K.尾结点,然后

leetCode解题报告5道题(九)

题目一:Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 分析: 题意给我们一个数字n, 和一个数字k,让我们求出从 1~~n中取出k个数所能得到的组合数 所

LeetCode解题源代码链接集锦二

15.Sort List--链表在O(nlogn),常数空间内完成排序 关键点:中间分裂链表,采用双指针归并排序     中间分裂链表的方法:快慢指针,快指针走两步,这样就可以找到中间的了 C++:http://blog.csdn.net/jiadebin890724/article/details/21334059 Java:http://blog.csdn.net/worldwindjp/article/details/18986737 LeetCode解题源代码链接集锦二,布布扣,bubu

LeetCode解题报告:Reorder List

Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 思路: 1.利用快慢两个指针将链表一分为二: 2.针对第二个子链表求倒序

leetcode解题目录

参考文献:http://blog.csdn.net/lanxu_yy/article/details/17848219 不过本文准备用超链接的方式连接到相应解答页面,不断更新中 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II BFS 哈希表 Surrounded Regions BFS 矩阵 Word Ladder BFS N/A Binary Tree Level Order Traversal BFS|前序遍历 队列 Binary Tre

LeetCode解题报告:Linked List Cycle &amp;&amp; Linked List Cycle II

Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follo

【算法题 14 LeetCode 147 链表的插入排序】

算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def insertionSortList(self, head): """ :type head: ListNode