Skip to content

Instantly share code, notes, and snippets.

@cengiz-io
Created July 9, 2020 15:20
Show Gist options
  • Save cengiz-io/af7f4aa3ca6385af5870822353a7ddb9 to your computer and use it in GitHub Desktop.
Save cengiz-io/af7f4aa3ca6385af5870822353a7ddb9 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
static void reverse(struct node** head_ref) {
struct node* tmp_prev = NULL;
struct node* current = *head_ref;
struct node* tmp_next;
while (current != NULL) {
tmp_next = current->next;
current->next = tmp_prev;
tmp_prev = current;
current = tmp_next;
}
*head_ref = tmp_prev;
}
int main(void) {
int i;
struct node *hn_ptr = NULL;
struct node *pn_ptr = NULL;
struct node *n_ptr = NULL;
for (i = 0; i < 10; i++) {
n_ptr = malloc(sizeof(struct node) * 1);
if (!n_ptr)
return EXIT_FAILURE;
n_ptr->data = i;
if (pn_ptr != NULL)
pn_ptr->next = n_ptr;
pn_ptr = n_ptr;
if (i == 0)
hn_ptr = n_ptr;
}
reverse(&hn_ptr);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment