61. Rotate List(M);19. Remove Nth Node From End of List(M)
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->NULL and k = 2, return 4->5->1->2->3->NULL.
- Total Accepted: 102574
- Total Submissions: 423333
- Difficulty: Medium
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode *rotateRight(ListNode *head, int k) { 12 if (head == nullptr || k == 0) return head; 13 int len = 1; 14 ListNode* p = head; 15 while (p->next) { // 16 len++; 17 p = p->next; 18 } 19 k = len - k % len; 20 p->next = head; // 21 for(int step = 0; step < k; step++) { 22 p = p->next; // 23 } 24 head = p->next; // 25 p->next = nullptr; // 26 return head; 27 } 28 };
16ms 19.35%
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution {// by guxuanqing@gmail.com 10 public: 11 ListNode* rotateRight(ListNode* head, int k) 12 { 13 if(NULL == head || NULL == head->next) return head;//null or one node 14 ListNode *p = NULL, *q = head; 15 int len = 1; 16 while (q->next)//cal the length of the list 17 { 18 ++len; 19 q = q->next; 20 } 21 q->next = head; 22 k %= len; 23 int tmp = len - k; 24 q = head; 25 while (--tmp) 26 { 27 q = q->next; 28 } 29 head = q->next; 30 q->next = NULL; 31 return head; 32 } 33 };
19. Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. Try to do this in one pass.
- Total Accepted: 169535
- Total Submissions: 517203
- Difficulty: Medium
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode *removeNthFromEnd(ListNode *head, int n) { 12 ListNode dummy(-1); 13 dummy.next = head; 14 ListNode *p = &dummy, *q = &dummy; 15 for (int i = 0; i < n; i++)//q先走n步 16 q = q->next; 17 while(q->next) { 18 p = p->next; 19 q = q->next; 20 } 21 ListNode *tmp = p->next; 22 p->next = p->next->next; 23 delete tmp; 24 return dummy.next; 25 } 26 };
6ms 64.75%
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution {// by guxuanqing@gmail.com 10 public: 11 ListNode* removeNthFromEnd(ListNode* head, int n) { 12 if(NULL == head || NULL == head->next) return NULL;//null or one node 13 ListNode dummy(-1); 14 dummy.next = head; 15 ListNode *p = &dummy, *q = &dummy; 16 int tmp = n; 17 while (tmp--) 18 { 19 q = q->next; 20 } 21 while (q->next) 22 { 23 q = q->next; 24 p = p->next; 25 } 26 ListNode *rnode = p->next; 27 p->next = rnode->next; 28 delete rnode; 29 return dummy.next; 30 } 31 };
9ms 23.47%
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!