title: Linked Lists delete node at position N
body: <p><strong>EDIT: Figured out the problem. Also if you found this through google or another search engine here is where I went wrong and how to fix it.</strong></p>
  
  <p>My deleteNode() method was moving through the list properly with the correct temp and keeping the head untouched. Where I was going wrong was in what I was returning as the result of the method. I was returning either temp or newNode which is incorrect because it goes through the list until it finds defined position. Once it finds that defined position it it would reassign the ->next pointer to point to the next->next> pointer which is correct but again I was returning the wrong thing. Because we had moved through the list using temp/NewNode we lost the header and we were returning the position we found and whatever was still in the next positions of the list.</p>
  
  <p>How we fix this is returning the head (which is what is passed into the method). The reason why this works is because we have to understand how LinkedLists work. The pointers of each node point to the next node. Ex. we have a linked list |A|<em>| - |B|</em>| - |C|<em>| - |D|</em>| - |E|<em>| - |F|</em>|</p>
  
  <p>If we want to delete Node C we move to node B using the temp pointer and then assign the B->next to temp->next->next Thus skipping over C node and assigning D node. </p>
  
  <p>NOTE: (From what I know this does not actually free the memory of C node so it isn't best practice because you can cause memory leaks this way) You should use the free() method on the C node.</p>
  
  <p>Here is the code I ended up using</p>
  
  <pre><code>struct node* DeleteNode(struct node* head, int pos) {
  
       struct node* temp = head;
       int length = LinkedListLength(temp);
       int i;
  
      if(pos &lt;= 0 || pos &gt; length){
          printf("ERROR: Node does not exist!\n");
      }else{
          if(pos == 1){
              head = head-&gt;next; //move from head (1st node) to second node
          }else{
              for(i = 1; i &lt; pos-1; ++i){ //move through list
                      temp = temp-&gt;next;
              }
              temp-&gt;next = temp-&gt;next-&gt;next;
          }
      }
      return head;
  }
  </code></pre>
  
  <p>Hopefully that helps understand how I went out fixing it. </p>
  
  <p>//////////////////////////////////////////////////////////////////////////////////////////////////<br>
  //////////////////////////////////////////////////////////////////////////////////////////////////<br>
  ORIGINAL POST<br>
  //////////////////////////////////////////////////////////////////////////////////////////////////<br>
  //////////////////////////////////////////////////////////////////////////////////////////////////  </p>
  
  <p>EDIT: <strong>Note: This is a homework assignment I have spent a few days (estimated 4 hours) programming it I am just stuck on this one part. You can view my attempt below</strong></p>
  
  <p>I've been able to insert and delete from begining/end however I can't seem to get my delete node at position N in linkedlist to work.</p>
  
  <p>My psuedocode looks like this:</p>
  
  <ol>
  <li>LinkedList: 1,3,5,7,9,23</li>
  <li>Grab LinkedList</li>
  <li>Create new struct node A = head</li>
  <li>Move through linkedlist until
  position</li>
  <li>Assign node to node->next</li>
  <li>return linkedlist</li>
  </ol>
  
  <p>EXAMPLE INPUT</p>
  
  <pre><code>Node structure 
  int data;
  struct node* next;
  
  int values[] = {1,3,5,7,9,23};
  struct node* llist = CreateList(values,6);
  
  llist = DeleteNode(llist, 1);
  llist = DeleteNode(llist, 5);
  llist = DeleteNode(llist, 3);
  </code></pre>
  
  <p>Which should leave the llist with the values 3, 5, 9 once the code has been run However, It is replacing the first node with a 0</p>
  
  <p>Actual Code:</p>
  
  <pre><code>struct node* DeleteNode(struct node* head, int pos) {
  
  struct node* temp = head;
  struct node* newNode = head;
  int length;
  int i;
  
  printf("DeleteNode: position = %d \nBefore: ", pos);
  PrintList(temp);
  
  if(pos &lt;= 0){ //node does NOT exist
      printf("ERROR: Node does not exist!\n");
  }else{ //node DOES exist
      length = LinkedListLength(temp);
  
      if(length &lt; pos){ //if length &lt; position Node does not exist
          printf("ERROR: Node does not exist!\n");
      }else{
          if(pos == 0){
              newNode = temp-&gt;next;
          }else if(pos == 1){
              newNode = temp-&gt;next;
          }else{
              for(i = 1; i &lt; pos; i++){
                  printf("i = %d\n", i);
                  temp = temp-&gt;next;
                  newNode-&gt;next;
              }
              if(temp-&gt;next == NULL){
                  newNode = NULL;
              }else{
                  newNode = temp-&gt;next;
              }
          }
      printf("After: ");
      PrintList(newNode);
      printf("\n");
      }
  }
  return newNode;
  }
  </code></pre>
  
  <p>EDIT #2: Code typo</p>
  
  <p>Thanks for any help in advance. From what I have concluded my problem is that I am not moving through the list properly but I am unsure as to why I am not.</p>