Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class node {
- public :
- int data;
- node * next;
- node(int data){
- this-> data = data;
- next = NULL;
- }
- };
- void insertHead(node* &head, int data){
- node * n = new node(data);
- n -> next = head;
- head = n;
- }
- void insert(node * &head, int data){
- node * n = new node(data);
- node * temp = head;
- while(temp -> next != NULL){
- temp = temp -> next;
- }
- temp->next = n;
- n -> next = NULL;
- }
- void print(node * &head){
- node * temp = head;
- while(temp != NULL){
- cout<<temp->data<<" ";
- temp = temp -> next;
- }
- }
- void reverseK(node* &head, int k){
- node * temp = head, *curr = head;
- node *tempPointer = NULL;
- node *prev = NULL;
- node * null_node = NULL;
- int count = 0, flag = 0;
- while(temp != NULL){
- prev = NULL;
- count = 0;
- while(temp != NULL){
- temp = curr->next;
- curr->next= prev;
- prev = curr;
- curr = temp;
- count++;
- if(count == 1){
- tempPointer = prev;
- }
- if(count == k){
- flag++;
- break;
- count = 0;
- }
- // cout<<flag<<endl;
- }
- if(flag == 1){
- null_node = head;
- head = prev;
- // print(head);
- // cout<<endl;
- //cout<<"**"<<endl;
- }
- else{
- null_node->next = prev;
- null_node = tempPointer;
- // print(head);
- // cout<<endl;
- }
- }
- }
- node* cycle(node* head){
- node * f = head;
- node * s = head;
- // cout<<" **"<<f->data<<" "<<s->data<<endl;
- while(f != NULL and f -> next != NULL){
- // cout<<" **"<<f->data<<" "<<f->next->data<<" "<<f->next->next->data<<"**"<<endl;
- f = f->next->next;
- s = s->next;
- // cout<<s->data<<" "<<f->data<<endl;
- if(f == s){
- f->next = NULL;
- return head;
- }
- }
- return head;
- }
- int main(){
- int n;
- node *head = NULL, *Head = NULL;
- int i = 0;
- // cin>>n;
- while( n != -1){
- if(i == 0)
- insertHead(head, n);
- else
- insert(head, n);
- cin>>n;
- i++;
- }
- node * h1 = cycle(head);
- print(h1);
- return 0;
- }
Add Comment
Please, Sign In to add comment