Partition list
Given a linked list and a value x, partition it such that all nodes less than X come before nodes greater than or equal to X.
You shoshould preserve the original relative order of the nodes in each of the two partitions.
For example,
Given1->4->3->2->5->2
And x = 3,
Return1->2->2->4->3->5
.
The question is relatively simple. Here we add a secondary node so that we don't have to discuss the first node in minutes.
Struct listnode {int val; listnode * Next; listnode (int x): Val (x), next (null) {}}; Class solution {public: listnode * partition (listnode * head, int X) {listnode * beforehead = new listnode (int_min); // secondary node to prevent operations on head nodes from beforehead-> next = head; listnode * P1 = beforehead, * P2 = beforehead, * P3 = head; while (P3) {If (P3-> Val <X & p2-> Val> = X) // P2-> Val> = X prevents the node from being inserted in situ. {P2-> next = P3-> next; P3-> next = p1-> next; p1-> next = P3; P1 = P3; P3 = P2-> next; continue;} If (P3-> Val <X) P1 = p1-> next; p2 = P3; P3 = P3-> next;} head = beforehead-> next; Delete beforehead; return head ;}};
Reverse nodes in K-group
Given a linked list, reverse the nodes of a linked list K at a time and return its modified list.
If the number of nodes is not a multiple of K then left-out nodes in the end shoshould remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list:1->2->3->4->5
For k = 2, you shoshould return:2->1->4->3->5
For k = 3, you shoshould return:3->2->1->4->5
The question is relatively simple. Here we add a secondary node so that we don't need to discuss the first node in minutes. The question of the linked list is relatively simple, but if there is no error in a short time, very difficult
Struct listnode {int val; listnode * Next; listnode (int x): Val (x), next (null) {}}; Class solution {public: listnode * reversekgroup (listnode * head, int K) {If (k <= 1 |! Head) return head; listnode * beforehead = new listnode (0); beforehead-> next = head; listnode * P1 = beforehead, * P2 = head, * P3 = head; while (P3) {int I; for (I = 0; I <K; ++ I) {If (! P3) break; P3 = P3-> next; // P3 indicates the next inverted Start Node} if (I <k) break; // indicates that there are no K nodes in the last step, directly return listnode * P = P2-> next; while (P! = P3) // reverse the relationship between P2 and P3 {P2-> next = p-> next; P-> next = p1-> next; P1-> next = P; P = P2-> next;} p1 = P2; P2 = P3;} head = beforehead-> next; Delete beforehead; return head ;}};
Remove duplicates from sorted List II
Given a sorted Linked List, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given1->2->3->3->4->4->5
, Return1->2->5
.
Given1->1->1->2->3
, Return2->3
.
These three questions are very simple, and a secondary node is used in the same place.
Struct listnode {int val; listnode * Next; listnode (int x): Val (x), next (null) {}}; Class solution {public: listnode * deleteduplicates (listnode * head) {If (! Head | head-> next = NULL) return head; listnode * beforehead = new listnode (0); beforehead-> next = head; listnode * P1 = beforehead, * P2 = head, * P3 = head-> next; while (P3) {If (P2-> val = P3-> Val) {While (P2-> val = P3-> Val) // P3 points to the next different node {P3 = P3-> next; If (! P3) break;} while (P1-> next! = P3) // Delete the node from P1 to P3 {p1-> next = P2-> next; Delete P2; P2 = p1-> next;} If (P3) p3 = P3-> next; continue;} p1 = P2; P2 = P3; P3 = P3-> next;} head = beforehead-> next; Delete beforehead; return head ;}};