当前位置:网站首页>Force button brushing question 61. rotating linked list
Force button brushing question 61. rotating linked list
2022-07-25 03:21:00 【GRT should always work hard】

Let the length of the list be n, If you move n Time , It will return to its original position . So every time you move, just move (k%n) Next time .
The idea is to first form the linked list into a ring , Then find the disconnected node .
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null || head.next == null){
return head;}
if(k == 0){
return head;}
ListNode tail = head;
int n = 1;
// First, find the last node of the linked list , And count the length of the linked list
while(tail.next != null){
tail = tail.next;
n++;
}
tail.next = head;
ListNode newTail = head;
for(int i = 0; i<(n-1-k%n); i++){
newTail = newTail.next;
}
// The new head node points to the position where the ring is disconnected
ListNode newHead = newTail.next;
newTail.next = null;
return newHead;
}
}
Be careful n The initial value of should be set to 1.
边栏推荐
- ES6 - study notes
- Detailed explanation of three factory modes
- Bgy development small example
- Database transactions (often asked)
- File permission management
- Learning record 10
- How to use two queues to simulate the implementation of a stack
- List title of force buckle summary
- Preliminary foundation JVM
- mysql_ Create temporary table
猜你喜欢

55k is stable, and the recommendation system will always drop God!

Banana pie bpi-m5 toss record (3) -- compile BSP

File permission management

Banana pie bpi-m5 toss record (2) -- compile u-boot
![[stm32f103rct6] can communication](/img/24/71509bd0d74d43ce4a79b8126478ff.jpg)
[stm32f103rct6] can communication

Li Kou 279 complete square - dynamic programming

Riotboard development board series notes (VII) -- the use of framebuffer

Learning record Xi

How to use two queues to simulate the implementation of a stack

Preliminary foundation JVM
随机推荐
Node queries the path of all files (files or folders) named filename under the target directory
Reasons for not sending requests after uni app packaging
Web -- JDBC tool class writing
Take a database statement note: when the field is empty, assign the default value to the result
Banana pie bpi-m5 toss record (3) -- compile BSP
Innobackupex parameter description
C language_ Defining structures and using variables
Preliminary foundation JVM
Learning record 12
Learning Record V
Bgy development small example
JS construction linked list
hello csdn
FLASH read / write problem of stm32cubemx
The relationship between private domain traffic and fission marketing. What is super app? Can our enterprise own it?
Learning record Xi
How does Jupiter notebook change themes and font sizes?
Unified return data format
MySQL configuration in CDH installation
Time formatting