61. Rotate List(M);19. Remove Nth Node From End of List(M)

61. Rotate 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 [email protected]
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 [email protected]
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%

时间: 2024-12-03 15:50:07

61. Rotate List(M);19. Remove Nth Node From End of List(M)的相关文章

[Leetcode][Python]19: Remove Nth Node From End of List

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 19: Remove Nth Node From End of Listhttps://oj.leetcode.com/problems/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 ex

leetCode 19. Remove Nth Node From End of List 链表

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 lis

19. Remove Nth Node From End of List(js)

19. Remove Nth Node From End of List Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes

leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法

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

19. Remove Nth Node From End of List(C++)

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. Solution : 1

Leetcode 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

[LeetCode]#19 Remove Nth Node From the End of list

从移动回来一个月,就一直在忙项目申请书的事情.通过写申请书,看新闻联播的新闻稿都开始顺眼了. 师兄师姐们已经开始找工作了,我还有一年时间,工作方面早作准备.机器学习方面继续推导常见的算法,刷一刷kaggle,做做特征工程什么的,算是应用了. 今天这道题其实并不难,但我是在Linux下写的,有些本地调试的方法和技巧想记录一下. 一.题目 Given a linked list, remove the nth node from the end of list and return its head

[LC] 19. Remove Nth Node From End of List

Given a linked list, remove the n-th node from the end of list and return its head. 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 w

LeetCode 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