P.S is a non professional and unfamiliar with C + + and algorithms. Starting from the introduction, he uses the Leetcode platform and draws on the ideas of many predecessors. The blog is only for recording.
Three questions in this issue's punch list: corresponding to < sword finger Offer 06 Print linked list from end to end > < sword finger Offer 24 Reverse linked list > < sword finger Offer 35 Copy of complex linked list >
Question 10: print linked list from end to end
Problem Description: enter the head node of a linked list, and return the value of each node from tail to head (returned by array).
Problem requirements: None
Solution idea: the requirement of this problem is to return the value of each node, not the reverse linked list: a natural idea is to use the stack to save the node value of the linked list from beginning to end, and then take out the value from the top of the stack to the bottom of the stack.
code:
/** 1. Definition for singly-linked list. 2. struct ListNode { 3. int val; 4. ListNode *next; 5. ListNode(int x) : val(x), next(NULL) {} 6. }; */ class Solution { public: vector<int> reversePrint(ListNode* head) { stack<int> temp; vector<int> result; int i = 0; while (head != nullptr){ temp.push(head->val); head = head->next; } while (!temp.empty()){ result.push_back(temp.top()); i=i+1; temp.pop(); } return result; } };
Note:
- Stack declaration: stack < int > temp; Array declaration vector < int > result; Add the value result. To the array push_ back(temp.top());
reference material:
Question 11: reverse linked list
Problem Description: define a function, input the head node of a linked list, invert the linked list and output the head node of the inverted linked list.
Problem requirements: None
Solution idea: this problem is equivalent to going further than the previous one. One idea is recursion.
Main strategy: recursion
A - > b - > C - > D - > e becomes a < - B < - C < - d < - E
Can be
a<-(b<-c<-d<-e)
mean
Changing a - > b to a < - B involves:
(1) a->b->a;
(2) a->nullptr; B - > A - > nullptr
Recursion starts at the tail.
code:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* new_head; ListNode* reverseList(ListNode* head) { if (head == nullptr || head->next == nullptr){ return head; //Give termination conditions } else{ new_head = reverseList(head->next); head->next->next = head; head->next = nullptr; //Every step is the head changing return new_head; } } };
Note:
- The idea of recursion and the position of operation should be mastered;
reference material:
Question 12: copy of complex linked list
Problem Description: please implement the copyRandomList function to copy a complex linked list. In a complex linked list, each node has a next pointer to the next node and a random pointer to any node or null in the linked list.
Problem requirements: None
Solution: this question is mainly based on the ideas of others (very good).
First copy: change a - > b - > C into a - > a '- > b' - > b '- > C' - > C '
This is equivalent to setting the next attribute of the linked list (copying the order of a.b.c);
Then assign a 'according to the random attribute of a.b.c b’. Random attribute of C ';
Finally, put a ' b’. c 'independent.
code:
/* // 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 == nullptr){ return nullptr; } for (Node* node = head;node != nullptr;node = node->next->next) { Node* newNode = new Node(node->val); newNode->next = node->next; node->next = newNode; } for (Node* node = head;node != nullptr;node = node->next->next) { if(node->random != nullptr) { Node* newNode = node->next; newNode->random = node->random->next; } } Node* new_head = head->next; for (Node* node = head;node != nullptr;node = node->next) { Node* newNode = node->next; node->next = node->next->next; newNode->next = (newNode->next != nullptr)? newNode->next->next:nullptr; } return new_head; } };
Note:
- This is a very interesting idea for reference.
reference material: