leetcode做题记录0061
发布日期:2021-05-07 13:48:28 浏览次数:19 分类:原创文章

本文共 684 字,大约阅读时间需要 2 分钟。

leetcode 0061

说明

只是为了记录一下,不求多快,也不深究。

会简要描述思路,代码中不写注释。

如碰到不会做的用了别人代码会在博客中标出。

题目描述

在这里插入图片描述

思路

先把整个链表变成循环链表,同时计算链表长度。

算一下应该移动多少步,即len-k%len.

移一下即可。

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */class Solution {       public ListNode rotateRight(ListNode head, int k) {   		if (head == null || head.next == null) {   			return head;		}		int len = 1;		ListNode tail = head;		while (tail.next != null) {   			len++;			tail = tail.next;		}		tail.next = head;		int move = k % len;		for (int i = 0; i < len - move; i++) {   			head = head.next;			tail = tail.next;		}		tail.next = null;		return head;	}}
上一篇:leetcode做题记录0062
下一篇:leetcode做题记录0060

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2025年03月28日 23时34分36秒