Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Reverese every K node in list
//Title of this code #include <iostream> #include <vector> struct node { int data; struct node* next; }; struct node *reverse (struct node *head, int k) { struct node* current = head; struct node* next; struct node* prev = NULL; int count = 0; // reverse first k nodes of the linked list while (current != NULL && count < k) { next = current->next; current->next = prev; prev = current; current = next; count++; } /* next is now a pointer to (k+1)th node Recursively call for the list starting from current. And make rest of the list as next of first node */ if(next != NULL) { head->next = reverse(next, k); } /* prev is new head of the input list */ return prev; } void push(struct node** head_ref, int new_data) { /* allocate node */ struct node* new_node = (struct node*) malloc(sizeof(struct node)); /* put in the data */ new_node->data = new_data; /* link the old list off the new node */ new_node->next = (*head_ref); /* move the head to point to the new node */ (*head_ref) = new_node; } /* Function to print linked list */ void printList(struct node *node) { while(node != NULL) { printf("%d -> ", node->data); node = node->next; } } /* Drier program to test above function*/ int main(void) { /* Start with the empty list */ struct node* head = NULL; /* Created Linked list is 1->2->3->4->5->6->7->8 */ push(&head, 8); push(&head, 7); push(&head, 6); push(&head, 5); push(&head, 4); push(&head, 3); push(&head, 2); push(&head, 1); printf("\n Given linked list \n"); printList(head); head = reverse(head, 3); printf("\n Reversed Linked list \n"); printList(head); return(0); }
run
|
edit
|
history
|
help
0
HeapSort
stl_sizeof.cc
Scemo
Policy based smart pointer
pointer array of functions
Value equal to index value
ONP is working!
Peg Grammar AST Parser Computer Language Interpreter
RegExpress
introduction c-types