# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param head: the list
# @param k: rotate to the right k places
# @return: the list after rotation
def rotateRight(self, head, k):
if head is None:
return head
cur = head
count = 1
# 计算链表长度
while cur.next:
cur = cur.next
count += 1
# 为节省代码量,这里是一个很有技巧的处理:用尾节点链接头结点
cur.next = head
# 此处,k为cur从尾节点到要断开部分的前驱需走的步数
k = count - k % count
# 找到前驱
while k != 0:
cur = cur.next
k -= 1
# 断开
head = cur.next
cur.next = None
# 因为首尾已经相连,所以直接返回前驱后面的那个节点即可,此处引用为head
return head
# write your code here
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有