You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class MyLinkedList {
public:
// 定义单链表
struct ListNode {
int val;
ListNode* next;
ListNode(int _val):val(_val), next(nullptr){}
}*head;
// 链表初始化,头节点为空
MyLinkedList() {
head = nullptr;
}
// 获取元素
int get(int index) {
// 索引无效返回-1
if (index < 0) return -1;
// 找到第index个点
auto p = head;
for (int i = 0; i < index && p; i ++) p = p->next;
if (!p) return -1;
return p->val;
}
// 在头节点前添加元素
void addAtHead(int val) {
auto cur = new ListNode(val);
cur->next = head;
head = cur;
}
// 在尾节点后添加元素
void addAtTail(int val) {
// 如果头节点不存在,新加入的元素就是头节点
if (!head) head = new ListNode(val);
else {
// 找到尾节点,在后面添加元素
auto p = head;
while (p->next) p = p->next;
p->next = new ListNode(val);
}
}
// 在第index点前添加一个元素
void addAtIndex(int index, int val) {
// 如果 index<=0,相当于在头节点添加元素
if (index <= 0) addAtHead(val);
else {
// 求链表总长度,如果 index 等于链表长度相当于在尾节点插入元素
int len = 0;
for (auto p = head; p; p = p->next) len ++;
if (index == len) addAtTail(val);
else if (index < len){
// 找到第 index 点的前驱节点,将其插入
auto p = head;
for (int i = 0; i < index - 1; i ++) p = p->next;
auto cur = new ListNode(val);
cur->next = p->next;
p->next = cur;
}
}
}
// 删除第index个节点
void deleteAtIndex(int index) {
int len = 0;
for (auto p = head; p; p = p->next) len ++;
if (index >= 0 && index < len){
// index 等于 0 相当于删除头节点
if (!index) head = head->next;
else {
auto p = head;
for (int i = 0; i < index - 1; i ++) p = p->next;
p->next = p->next->next;
}
}
}
};
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList* obj = new MyLinkedList();
* int param_1 = obj->get(index);
* obj->addAtHead(val);
* obj->addAtTail(val);
* obj->addAtIndex(index,val);
* obj->deleteAtIndex(index);
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if (!head) return head;
// 旋转的次数如果是链表长度的倍数,相当于没有旋转
int len = 0;
for (auto p = head; p; p = p->next) len ++;
k %= len;
if (!k) return head;
auto p = head;
for (int i = 0; i < len - k - 1; i ++) p = p->next;
auto tail = head;
while (tail->next) tail = tail->next;
tail->next = head;
auto new_head = p->next;
p->next = nullptr;
return new_head;
}
};
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) {
val = _val;
next = NULL;
random = NULL;
}
};
*/
class Solution {
public:
Node* copyRandomList(Node* head) {
if (!head) return NULL;
// 1.将原链表每个节点复制一份接在原节点后
// 例如:1->2->3->4 => 1->1->2->2->3->3->4->4
auto p = head;
while (p){
auto copy = new Node(p->val);
copy->next = p->next;
p->next = copy;
p = copy->next;
}
// 2.p->next->random = p->random->next
for (auto p = head; p; p = p->next->next){
if (p->random)
p->next->random = p->random->next;
}
// 3.将新链和原链拆分,将原链恢复原状,将新链返回
auto cur = head->next, pre = head, res = head->next;
while (cur->next){
pre->next = pre->next->next;
cur->next = cur->next->next;
pre = pre->next;
cur = cur->next;
}
pre->next = NULL;
return res;
}
};