title: I am getting an segmentation fault error in line head=NULL
body: <p>I didn't understand why i am getting error in line head=NULL I am just pointing head towards NULL</p>
  
  <p>I have also tried creating a new node for head and then pointing towards NULL but still it doesn't work</p>
  
  <p><strong>Code show here</strong></p>
  
  <pre><code>#include &lt;bits/stdc++.h&gt;
  
  using namespace std;
  struct node
  {
    int data;
    node *next;
  };
  
   node *head=NULL;
   node *tail=NULL;
  
  
   void createnode(int value)
    {
    node *temp=new node;
    temp-&gt;data=value;
    temp-&gt;next=NULL;
    if(head==NULL)
    {
      head=temp;
      tail=temp;
      temp=NULL;
    }
    else
    { 
      tail-&gt;next=temp;
      tail=temp;
    }
  }
  void deletek(int n,int k)
  {
      if(head==NULL)
      return;
      node *temp;
      temp=head;
      while(temp-&gt;data&lt;temp-&gt;next-&gt;data)
      {
          head=temp-&gt;next;
          temp=temp-&gt;next;
          k--;
      }
      while(temp!=NULL&amp;&amp;k!=0)
      {
          if(temp-&gt;next-&gt;data&lt;temp-&gt;next-&gt;next-&gt;data)
          {
              temp-&gt;next=temp-&gt;next-&gt;next;
              k--;
          }
          temp=temp-&gt;next;
      }
  }
  void display()
  {
      node *temp=head;
      while(temp!=NULL)
      {
          cout&lt;&lt;temp-&gt;data&lt;&lt;" ";
          temp=temp-&gt;next;
      }
      cout&lt;&lt;endl;
  
  }
  
  int main() {
  int t;
  cin&gt;&gt;t;
  int n,k;
  
  for(int i=0;i&lt;t;i++)
  {
      cin&gt;&gt;n&gt;&gt;k;
      int arr[n];
  
      for(int j=0;j&lt;n;j++)
      {
          cin&gt;&gt;arr[j];
          createnode(arr[j]);
      }
      deletek(n,k);
  
      display();
      head=NULL;
  
  
  }
  
  }
  </code></pre>
  
  <p>I expect that whenever the line head=NULL runs the previous linked list should get delete and I should be able to enter elements in the linked list from the beginning</p>