[Small Vegetable Chicken Brush Inscription]---Linked List
Sword Pointer Offer 06. Print linked list from end to end
Sword refers to Offer 06. Print the linked list from end to head Input
the head node of a linked list, and return the value of each node from end to head in turn (return in an array).
Input: head = [1,3,2]
Output: [2,3,1]
Reverse implementation
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
vector<int> v;
ListNode* cur=head;
while(cur)
{
v.push_back(cur->val);
cur=cur->next;
}
reverse(v.begin(),v.end());
return v;
}
};
Use the characteristics of the stack
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
vector<int> v;
stack<int> st;
ListNode* cur=head;
while(cur)
{
st.push(cur->val);
cur=cur->next;
}
while(!st.empty())
{
int top=st.top();
v.push_back(top);
st.pop();
}
return v;
}
};
recursive implementation
class Solution {
public:
vector <int> v;
vector<int> reversePrint(ListNode* head) {
// vector <int> v;
// if(head==nullptr)
// {
// return v;
// }
// v=reversePrint(head->next);
// v.push_back(head->val);
// return v;
vector<int> v;
if(head!=nullptr)
{
if(head->next!=nullptr)
{
v=reversePrint(head->next);
}
v.push_back(head->val);
}
return v;
}
};
Sword Pointing to Offer 24. Reverse Linked List
Sword refers to Offer 24. Reversing linked list
Define a function, input the head node of a linked list, reverse the linked list and output the head node of the reversed linked list.
Example:
Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL
head insertion
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* newhead=nullptr;
ListNode* cur=head;
while(cur)
{
//保存下一个节点
ListNode* next=cur->next;
cur->next=newhead;
newhead=cur;
cur=next;
}
return newhead;
}
};
Sword refers to Offer 35. Copy of complex linked list
Sword Pointing to Offer 35. Copying a complex linked list
Please implement the copyRandomList function to copy a complex linked list. In a complex linked list, each node has a next pointer pointing to the next node, and a random pointer pointing to any node in the linked list or null. There is a detailed explanation of the second method in
my other article
Example 1: Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]] Output: [[
7,null],[13, 0],[11,4],[10,2],[1,0]]
hash
Exchange space for time: copy node N' in the original linked list, form a key-value pair <N, N'> with the original node and add it to the map collection, then recursively copy the next node and random pointer
class Solution {
public:
unordered_map<Node*,Node*> map;
Node* copyRandomList(Node* head) {
if(head==nullptr)
{
return nullptr;
}
if(map.count(head)<1)
{
Node* copyNode=new Node(head->val);
//map.insert({head,copyNode});
map.insert(std::make_pair(head,copyNode));//添加到map中
copyNode->next=copyRandomList(head->next);//递归复制下一节点
copyNode->random=copyRandomList(head->random);///递归复制随机指针
}
return map[head];//返回pair.second的引用
}
};
iteration + node splitting
/*
// 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:
//1.复制原链表的每个节点并添加的原节点的下一节点
void copyNodeAndNext(Node* head)
{
Node* cur=head;
while(cur)
{
Node* copyNode=new Node(cur->val);
copyNode->next=cur->next;
cur->next=copyNode;
cur=copyNode->next;
}
}
//2.复制原链表节点的随机指针
void copyNodeAndRandom(Node* head)
{
Node* cur=head;
while(cur)
{
Node* copyNode=cur->next;
//如果原链表的随机指针不为空,那么复制节点的随机指针指向的节点
//就是原链表随机指针指向节点的下一个节点
if(cur->random)
{
copyNode->random=cur->random->next;
}
cur=copyNode->next;
}
}
//3.将复制好的链表拆下来
Node* deatchList(Node* head)
{
Node* cur=head;
Node* copyhead=new Node(-1);//创建头结点,方便拆分组合
Node *copytail=copyhead;
while(cur)
{
Node* copyNode=cur->next;
Node* next=copyNode->next;
//保留原链表
cur->next=next;
//分割出新链表
copytail->next=copyNode;
copytail=copytail->next;
cur=next;
}
Node* newhead=copyhead->next;
delete copyhead;
return newhead;
}
Node* copyRandomList(Node* head) {
copyNodeAndNext(head);
copyNodeAndRandom(head);
return deatchList(head);
}
};