Day 40: Reversing My Linked List (Sort of)

Continued my slow progress through linked lists. I made another improvement to the original hard coded linked list. By doing that it spurred a question. Currently because of the preferences of the tutorial I am following the list is set up to work backwards instead of forwards. I was thinking about how I can change that and I came up with a few interesting solutions.

TLDR;

Okay, so here are the highlights of what I did:

  • Continued work on the “Technical Questions” section of the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. Continued my study of Linked Lists. I am trying to improve on my first example while noting what I am changing and how the improvements help improve the quality of my code. I made the changed needed for the declaration of my pointers. Additionally I started to play with the construction of the linked lists to see if I am really starting to understand how it’s constructed or not. If the list is currently constructed with the first node being at the end of the list, then I should be able to flip it and make the first node appear at the front of the list. At least in theory LOL.

Notes on Flipping the direction of my Linked List in C

// Current Linked List Construction

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

struct node {
  int value;
  struct node* next;
};

// Allows us to write out node_t instead of struct node every time we use that type.
typedef struct node node_t;

node_t *create_new_node(int value){
  node_t *result = malloc(sizeof(node_t));
  result->value = value;
  result->next = NULL;
  return result;
}

void printList(node_t *head){
  node_t *temporary = head;

  while(temporary != NULL){
    printf("%d - ", temporary->value);
    temporary = temporary->next;
  }
  printf("\n");
}

int main(){

  // The original tutorial left these unassigned but after learning
  // about garbage values being used by default it is better
  // practice to assign all pointers a NULL value or an actual
  // address when declaring a pointer variable. This will limit the
  // existance of garbage values that can be confused with actual
  // values that matter in our code.
  node_t *head = NULL;
  node_t *tmp = NULL;

  // Adding a for loop to constuct as many nodes as we may want in
  // our linked list instead of manually declaring each.
  for(int i = 0; i < 10; ++i){
    tmp = create_new_node(i);
    tmp->next = head;
    head = tmp;
  }

  printList(head);

  return 0;
}

And the output is:

9 - 8 - 7 - 6 - 5 - 4 - 3 - 2 - 1 - 0 -

With the reversal I changed the for loop:

for(int i = 0; i < 10; ++i){
  if(i == 0){
    tmp = create_new_node(i);
    head = tmp;
  } else {
    tmp->next = create_new_node(i);
    tmp = tmp->next;
  }
}

And I got this output:

0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 -

Another alternative that I initially thought of would be to reverse the loop:

  for(int i = 9; i >= 0; --i){
    tmp = create_new_node(i);
    tmp->next = head;
    head = tmp;
  }

And the same output I got was:

0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 -

Conclusion

That’s all for today. This is my sixth round of the “#100daysofcode” challenge. I will be continuing my work from round five into round six. I am currently working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal is to become more familiar with algorithms and data structures. This goal was derived from my goal to better understand operating systems and key programs that I use in the terminal regularly e.g. Git. This goal was in term derived from my desire to better understand the fundamental tools used for coding outside of popular GUIs. This in turn was derived from my desire to be a better back-end developer.

I have no idea if my path is correct but I am walking down this road anyways. Worst case scenario I learn a whole bunch of stuff that will help me out on my own personal projects.