Skip to content

jayeshhS/code2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

code2

#include <stdio.h> #include <stdlib.h>

// Define a structure for a node in the linked list struct Node { int data; struct Node* next; };

// Function to insert a node at the beginning of the linked list void insertAtBeginning(struct Node** head_ref, int new_data) { // Allocate memory for a new node struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); // Assign the data to the new node new_node->data = new_data; // Set the next of the new node to the current head new_node->next = *head_ref; // Move the head to point to the new node *head_ref = new_node; }

// Function to print the linked list void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } printf("\n"); }

// Function to reverse the linked list void reverseList(struct Node** head_ref) { struct Node* prev = NULL; struct Node* current = head_ref; struct Node next = NULL;

while (current != NULL) {
    // Store next node
    next = current->next;
    // Reverse current node's pointer
    current->next = prev;
    // Move pointers one position ahead
    prev = current;
    current = next;
}
// Make the head point to the last node (which was the first node before reversal)
*head_ref = prev;

}

int main() { struct Node* head = NULL;

// Insert elements into the linked list
insertAtBeginning(&head, 5);
insertAtBeginning(&head, 4);
insertAtBeginning(&head, 3);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 1);

printf("Original linked list: ");
printList(head);

// Reverse the linked list
reverseList(&head);

printf("Reversed linked list: ");
printList(head);

return 0;

}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published