C Exercises: Reverse alternate k nodes of a singly linked list
C Singly Linked List : Exercise-41 with Solution
Write a C program to reverse alternate k nodes of a given singly linked list.
Sample Solution:
C Code:
#include<stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Function to create a new node in the linked list
struct Node* newNode(int data) {
struct Node* node = (struct Node*) malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
return node;
}
Node* reverse_Alt_K_Nodes(Node* head, int k) {
Node* current = head;
Node* prev = NULL;
Node* next = NULL;
int count = 0;
/* reverse the first k nodes of the linked list */
while (current != NULL && count < k) {
next = current->next;
current->next = prev;
prev = current;
current = next;
count++;
}
/* head points to k+1th node */
if (head != NULL) {
head->next = current;
}
/* skip next k nodes */
count = 0;
while (count < k - 1 && current != NULL) {
current = current->next;
count++;
}
/* recursively call the function on the remaining list */
if (current != NULL) {
current->next = reverse_Alt_K_Nodes(current->next, k);
}
/* prev now points to the head of the reversed k nodes */
return prev;
}
void printList(Node* head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
int main() {
// Create a sample linked list
struct Node* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
head->next->next->next->next = newNode(5);
head->next->next->next->next->next = newNode(6);
head->next->next->next->next->next->next = newNode(7);
head->next->next->next->next->next->next->next = newNode(8);
printf("Original List: ");
printList(head);
head = reverse_Alt_K_Nodes(head, 2);
printf("\nReverse alternate k (k=2) nodes of the said singly linked list:\n");
printList(head);
head = reverse_Alt_K_Nodes(head, 3);
printf("\nReverse alternate k (k=3) nodes of the said singly linked list:\n");
printList(head);
head = reverse_Alt_K_Nodes(head, 4);
printf("\nReverse alternate k (k=4) nodes of the said singly linked list:\n");
printList(head);
return 0;
}
Sample Output:
Original List: 1 2 3 4 5 6 7 8 Reverse alternate k (k=2) nodes of the said singly linked list: 2 1 3 4 6 5 7 8 Reverse alternate k (k=3) nodes of the said singly linked list: 3 1 2 4 6 5 8 7 Reverse alternate k (k=4) nodes of the said singly linked list: 4 2 1 3 6 5 8 7
Flowchart :


C Programming Code Editor:
Previous: Swap every two adjacent nodes of a singly linked list.
Next: Find the point at which two singly linked lists intersect.
What is the difficulty level of this exercise?
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join