当前位置:网站首页>LeetCode-61
LeetCode-61
2022-07-05 06:16:00 【GreedySnaker】
Give you a list of the head node head , Rotate the list , Move each node of the list to the right k A place .
At first, I thought about finding a new head according to the number of moves , New tail , Direct link . Later, we found that the circular linked list can record the node precursor and follower with fewer variables .
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k)
{
// Does not meet the conditions or there is only one node
if (head == NULL || head->next == NULL || k == 0)
{
return head;
}
// Record the number of linked list elements , Find the tail node
ListNode* temp = head;
int num = 1;
while (temp->next != NULL)
{
temp = temp->next;
num++;
}
//k When it is greater than the number of elements , Just deal with the remainder
k = k % num;
if (k == 0)
{
return head;
}
// Connect the single linked list into a circular list
temp->next = head;
// According to the number of shifts to the right , Cut off the ring linked list at the corresponding position , Return to new header
temp = head;
for (int i = 1; i < (num - k); i++)
{
temp = temp->next;
}
ListNode* res = temp->next;
// Segmented ring linked list
temp->next = NULL;
return res;
}
};
边栏推荐
- Regulations for network security events of vocational group in 2022 Guizhou Vocational College skill competition
- MySQL advanced part 1: stored procedures and functions
- C Primer Plus Chapter 15 (bit operation)
- Appium基础 — 使用Appium的第一个Demo
- [leetcode] day94 reshape matrix
- LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
- MySQL advanced part 2: SQL optimization
- 1041 Be Unique
- 1040 Longest Symmetric String
- 【Rust 笔记】15-字符串与文本(上)
猜你喜欢
数据可视化图表总结(一)
Groupbykey() and reducebykey() and combinebykey() in spark
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
SQLMAP使用教程(一)
Arduino 控制的 RGB LED 无限镜
Chapter 6 relational database theory
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
SPI details
7. Processing the input of multidimensional features
随机推荐
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
The difference between CPU core and logical processor
4. Object mapping Mapster
1040 Longest Symmetric String
Leetcode recursion
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
Sword finger offer II 058: schedule
leetcode-31:下一个排列
Traversal of leetcode tree
Daily question 1342 Number of operations to change the number to 0
传统数据库逐渐“难适应”,云原生数据库脱颖而出
WordPress switches the page, and the domain name changes back to the IP address
1039 Course List for Student
MIT-6874-Deep Learning in the Life Sciences Week 7
Sqlmap tutorial (1)
Binary search template
实时时钟 (RTC)
One question per day 1447 Simplest fraction
Doing SQL performance optimization is really eye-catching
Redis publish subscribe command line implementation