【LeetCode with Python】 Rotate List
2014-08-29 18:34
211 查看
博客域名:http://www.xnerv.wang
原题页面:https://oj.leetcode.com/problems/rotate-list/
题目类型:
难度评价:★
本文地址:/article/1377489.html
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given
return
原题页面:https://oj.leetcode.com/problems/rotate-list/
题目类型:
难度评价:★
本文地址:/article/1377489.html
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->NULLand k =
2,
return
4->5->1->2->3->NULL.
class Solution: # @param head, a ListNode # @param k, an integer # @return a ListNode def rotateRight(self, head, k): if None == head or None == head.next: return head cur_head = head cur = head while k > 0: cur = cur.next if None == cur: return head k -= 1 cur_tail = cur cur_head = cur.next if None == cur_head: return head cur = cur_head while None != cur.next: cur = cur.next cur.next = head cur_tail.next = None return cur_head
相关文章推荐
- 【LeetCode with Python】 Swap Nodes in Pairs
- 【LeetCode with Python】 Single Number
- 【LeetCode with Python】 Best Time to Buy and Sell Stock II
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- 【LeetCode with Python】 Regular Expression Matching
- 【LeetCode with Python】 Decode Ways
- 【LeetCode with Python】 LRU Cache
- 【LeetCode with Python】 Binary Tree Preorder Traversal
- 【LeetCode with Python】 Insertion Sort List
- 【LeetCode with Python】 Generate Parentheses
- 【LeetCode with Python】 Same Tree
- 【LeetCode with Python】 Pow(x, n)
- 【LeetCode with Python】 Remove Duplicates from Sorted Array
- 【LeetCode with Python】 Binary Tree Inorder Traversal
- 【LeetCode with Python】 Maximum Subarray
- 【LeetCode with Python】 String to Integer (atoi)
- 【LeetCode with Python】 Remove Duplicates from Sorted List
- 【LeetCode with Python】 Merge Two Sorted Lists
- 【LeetCode with Python】 Maximum Depth of Binary Tree
- 【LeetCode with Python】 Remove Element