title: Delete node from Linked List C++
body: <p>I am working on iterative delete function that deletes node from a linked list, I think that the code is supposed to work fine. But when I can't use "delete" to delete the first Node of the List.
  The code is:
      #include 
      using namespace std;</p>
  
  <pre><code>struct Node
  {
      int data;
      Node* next;
  };
  
  Node* GetNewNode(int data)
  {
      Node* newNode = new Node;
      newNode-&gt;data = data;
      newNode-&gt;next = NULL;
      return newNode;
  }
  
  Node* Insert(Node *root, int data)
  {
      if (root == NULL)
      {
          root = GetNewNode(data);
  
      }
      else
          root-&gt;next = Insert(root-&gt;next, data);
      return root;
  }
  
  void Delete_k(Node *root, int k)
  {
  
      int i = 0;
      Node* P = new Node;
      if (k == 1)
      {
          P = root;
          root = root-&gt;next;
          delete P;
  
      }
      else
      {
          for (int i = 1; i &lt;= k - 2; i++)
          {
              root = root-&gt;next;
          }
          root-&gt;next = root-&gt;next-&gt;next;
      }
  }
  
  void Output(Node* root)
  {
      if (root == NULL)
      {
          root = root-&gt;next;
      }
      while (root != NULL)
      {
          cout &lt;&lt; root-&gt;data &lt;&lt; " ";
          root = root-&gt;next;
      }
  }
  
  int main()
  {
      int n, a, pos;
      Node* root = NULL;
      cout &lt;&lt; "Input your list hear: ";
      cin &gt;&gt; n;
      while (n &gt; 0)
      {
          root = Insert(root, n);
          cin &gt;&gt; n;
      }
      Output(root);
      cout &lt;&lt; "\nDelete Pos?: ";
      cin &gt;&gt; pos;
      Delete_k(root, pos);
      Output(root);
  }
  </code></pre>
  
  <p>I have problem in this</p>
  
  <pre><code>void Delete_k(Node *root, int k)
  {
  
      int i = 0;
      Node* P = new Node;
      if (k == 1)
      {
          P = root;
          root = root-&gt;next;
          delete P;
  
      }
      else
      {
          for (int i = 1; i &lt;= k - 2; i++)
          {
              root = root-&gt;next;
          }
          root-&gt;next = root-&gt;next-&gt;next;
      }
  }
  </code></pre>