#include <iostream>Using namespace Std;struct node{intVal struct Node*next; Node (int x=int()): Val (x),Next(NULL) {}};struct list{List () {head=null; } void Insert (int x) {if(Head==null) {head = new Node (x); }Else{Node*p= head; Node*s= New Node (x); while(p->Next!=null) {p=p->Next; } p->Next=s; } }intSize () {Node*p= head;returnSize (P); }intSize (node* p) {if(P==null)return 0;returnSize (p->Next)+1; } void Inverted (intAintb) {inti = A-1;intj = B1;intsize = size ();if(Size<a | | b>size)return; Node*p= head->Next; Node*q= head; Node*prve= NULL;//Saves the reversed node segment before a node. Node*last= NULL;//Saves a node behind a reversed node segment.if(a==1) { while(j) { Last=p->Next; P->Next=Q;Q=p; p= Last; j--; } head->Next= P; Head =Q; }Else{p = head;Q= head; while(i) {prve = P; P=p->Next; i--; } while(j) {Q=Q-Next; j--; } Last=Q-Next; Node*save=null; Node*m= p->Next; Node*n= P; while(p!= Last) {save = p->Next; P->Next=m;m=p; P=save; } prve->Next=m; N->Next= Last; }} void Inverted (intk) {Node*p= head;inti =1;intCount =0; while(P!=null) {p=p->Next; count++; } while(count>0) {Inverted (i,i+k-1);if(count>k) {count-=k; I+=k;if(count<=k)Continue; }if(count<=k) {i+=count; Count-=k; }}} void Show () {Node*p= head; while(P!=null) {cout<<p->val<<" "; P=p->Next; } cout<<endl; } Private:node*head;};intMain () {intItem; List List; while(cin>>item,item!=-1) {list. Insert (Item); }//1 2 3 4 5 6 7 8List. Inverted (2); List. Show ();return 0;}
Complex linked list of replication ideas:
struct node{
Node *next;
Node *other;//randomly points to any other node, or points to null.
int Val;
};
We now have the original linked list, such as (1,2,3,4,5), each node after the replication of an identical node, is (1,1,2,2,3,3,4,4,5,5), and then the heart of the other pointer to the new corresponding node (even the new node), and then delete the odd node, You get a copy of the complex list.
To provide you with a learning C + + and query C + + online site, quite with a app,www.cplusplus.com.
C + + linked list K nodes of K node inversion ((1,2,3,4), if K is 2, the reversal result is (2,1,4,3))