add: 220311 [cpp]
This commit is contained in:
parent
915e9e6ca2
commit
1b506210f8
|
|
@ -0,0 +1,62 @@
|
|||
#include <iostream>
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
*/
|
||||
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode* next;
|
||||
|
||||
explicit ListNode(int x = 0, ListNode* next = nullptr) : val(x), next(next) {}
|
||||
};
|
||||
|
||||
ListNode* construct(int x) {
|
||||
return new ListNode(x);
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
ListNode* construct(int x, Ts... xs) {
|
||||
return new ListNode(x, construct(xs...));
|
||||
}
|
||||
|
||||
/**
|
||||
* 61. Rotate List
|
||||
* Given the head of a linked list, rotate the list to the right by k places.
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
static ListNode* rotateRight(ListNode* head, int k) {
|
||||
if (!head || !head->next)
|
||||
return head;
|
||||
|
||||
int n = 0;
|
||||
for (auto* ptr = head; ptr; ptr = ptr->next)
|
||||
++n;
|
||||
int adv = (n - (k % n)) % n;
|
||||
|
||||
auto* ptr = head;
|
||||
for (int i = 0; i < adv; ++i)
|
||||
ptr = ptr->next;
|
||||
auto* ptr2 = ptr;
|
||||
while (ptr2->next)
|
||||
ptr2 = ptr2->next;
|
||||
ptr2->next = head;
|
||||
for (auto* ptr3 = head;; ptr3 = ptr3->next) {
|
||||
if (ptr3->next == ptr) {
|
||||
ptr3->next = nullptr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
auto* head = construct(1,2);
|
||||
head = Solution::rotateRight(head, 0);
|
||||
for (auto* ptr = head; ptr; ptr = ptr->next)
|
||||
std::cout << ptr->val << " ";
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2203)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2203 220311-CN.cpp)
|
||||
ADD_EXECUTABLE(2203 220311.cpp)
|
||||
|
|
|
|||
Loading…
Reference in New Issue