[LeetCode]题解(python):061-Rotate list



题目来源



https://leetcode.com/problems/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->NULL and k = 2,
return 4->5->1->2->3->NULL.



题意分析



Input:a list of node

Output:a list of node shift to right

Conditions:链表右移



题目思路



总体思路是链表右移,关键是右移k可能大于实际长度,所以先遍历一次求长度,然后再求模得到真正右移数量

PS:对于链表操作,每次在前面添加一个节点这个方法很好用,特别注重边界条件

PS2:尽量用xrange代替range



AC代码(Python)


 1 __author__ = ‘YE‘
 2
 3 # Definition for singly-linked list.
 4 class ListNode(object):
 5     def __init__(self, x):
 6         self.val = x
 7         self.next = None
 8
 9 class Solution(object):
10     def rotateRight(self, head, k):
11         """
12         :type head: ListNode
13         :type k: int
14         :rtype: ListNode
15         """
16         if k == 0 or head == None:
17             return head
18
19         addFirst = ListNode(0)
20         addFirst.next = head
21         # move variable
22         p = addFirst
23         #the length of list
24         count = 0
25         while p.next != None:
26             p = p.next
27             count += 1
28         p.next = addFirst.next
29         #the real step to shift right
30         step = count - (k % count)
31         p = addFirst.next
32         for i in xrange(1, step):
33             p = p.next
34         head = p.next
35         p.next = None
36
37         return head
时间: 2024-12-15 18:04:30

[LeetCode]题解(python):061-Rotate list的相关文章

【LeetCode with Python】 Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 题意分析: 顺时针90度转动一个二维矩阵,要求in-place的算法,也就不是不能另外开一个新的矩阵然后把原矩阵上的元素一个个对应复制过来,而要在原矩阵上进行操作. 算法分析: 目前能想到的有两种方法. 第一种比较直观,首

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

[LeetCode 题解]: 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->NULL and k = 2,return 4->5->1->2->3->NULL. 题意: 翻转链表. 思路: 首先需要读懂题意.题目的描述有问题,应该是将链表的最后k个元素移动到链表的头部. 这道题的本质就是寻找链表的

(leetcode题解)Pascal's Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

[leetcode]Candy @ Python

原题地址:https://oj.leetcode.com/problems/candy/ 题意: There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy

[LeetCode 题解]: Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

LeetCode题解

Reverse Words in a String 考虑几个特殊的情况1.若字符窜s="  "2.字符窜s=“a  b  d     e”3.字符窜s=“ a” class Solution { public: void reverseWords(string &s) { int i; int cas=0; string st[100]; s+=' '; for(i=0;i<s.size();i++) { if(i==0 && s[0]==' ') con

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du