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、尾结点,然后再去定位。注意,题目中提到前置条件k是非负数,所以要判断k是否越界。最终旋转时注意不要形成循环链表,导致打印链表时死循环!

时间: 2024-09-29 18:24:23

Leetcode解题-链表(2.2.6)RotateList的相关文章

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.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 t

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解题报告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 && 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