Question : reverse list in C
Answered by : pleasant-porcupine-k21t1ro4oqmq
#include <stdio.h>
void main() { int List[5] = { 1, 2, 3, 4, 5 }; int starting_index = 0; int end_index = 4; while (starting_index < end_index) { int temp = List[starting_index]; List[starting_index] = List[end_index]; List[end_index] = temp; starting_index += 1; end_index -= 1; } for (int i = 0; i < 5; i++) { printf("%d, ", List[i]); }
}
Source : | Last Update : Sat, 23 Apr 22
Question : Reversing the List in c
Answered by : darkdevilor-il6f6ra6zfec
// Iterative C program to reverse a linked list
#include <stdio.h>
#include <stdlib.h>
/* Link list node */
struct Node {
int data;
struct Node* next;
};
/* Function to reverse the linked list */
static void reverse(struct Node** head_ref)
{
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next = NULL;
while (current != NULL) {
// Store next
next = current->next;
// Reverse current node's pointer
current->next = prev;
// Move pointers one position ahead.
prev = current;
current = next;
}
*head_ref = prev;
}
/* Function to push a node */
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node
= (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
/* Function to print linked list */
void printList(struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}
/* Driver code*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);
printf("Given linked list\n");
printList(head);
reverse(&head);
printf("\nReversed linked list \n");
printList(head);
getchar();
}
Source : https://www.geeksforgeeks.org/reverse-a-linked-list/ | Last Update : Wed, 23 Nov 22