title: Will there be a memory leak in this Linked list
body: <p>Hi Currently i have been honing my skill in the data structure so that i can become a good Game Developer, i am Learning Linked list and made a Linked list Program with Insertion ,Deletion and Recursive insertion and Reversing a Linked list Can you Guys tell me am i here Clearing the assigned memory Created with the new Operator Correctly, I am getting the Desired Output but i am worried about memory leak. ... please be gentle aim Still learning.</p>
  <pre><code>class Node
  {
      int data;
      Node *Next;
  
  public:
      int GetData()
      {
          return data;
      }
      void SetData(int Data)
      {
          data = Data;
      }
  
      Node *GetNext()
      {
          return Next;
      }
  
      void SetNext(Node *next)
      {
          Next = next;
      }
  };
  
  void Insert(Node **Head, int x)
  {
      Node *temp = new Node();
  
      temp-&gt;SetData(x);
      temp-&gt;SetNext(*Head);
  
      *Head = temp;
  }
  
  void InsertAt(Node **Head, int x, int n)
  {
      if (n == 0)
      {
          std::cout &lt;&lt; &quot;The Given data at 'n' cannot be assigned to null\n&quot;;
      }
  
      Node *temp = new Node();
      temp-&gt;SetData(x);
      if (n == 1)
      {
          temp-&gt;SetNext(nullptr);
          *Head = temp;
          return;
      }
  
      Node *temp2 = *Head;
      if (Head == nullptr)
      {
          std::cout &lt;&lt; &quot;The Given data cannot be assigned to null\n&quot;;
      }
  
      for (int i = 0; i &lt; n - 2; i++)
      {
          temp2 = temp2-&gt;GetNext();
      }
  
      temp-&gt;SetNext(temp2-&gt;GetNext());
      temp2-&gt;SetNext(temp);
  }
  
  void AppendList(Node **Head, int Data)
  {
      Node *temp = new Node();
  
      Node *LastNode = *Head;
  
      temp-&gt;SetData(Data);
      temp-&gt;SetNext(nullptr);
  
      if (*Head == nullptr)
      {
          *Head = temp;
          return;
      }
      // else Traverse till last node.
  
      while (LastNode-&gt;GetNext() != nullptr)
      {
          LastNode = LastNode-&gt;GetNext();
      }
      LastNode-&gt;SetNext(temp);
  }
  
  void DeleteNode(Node **Head, int n)
  {
      Node *temp = *Head;
  
      if (n == 1)
      {
          *Head = temp-&gt;GetNext();
          std::cout &lt;&lt; &quot;\nAfter Deletion of Head Node\n&quot;;
          return;
      }
  
      for (int i = 0; i &lt; n - 2; i++)
      {
          temp = temp-&gt;GetNext();
      }
      Node *temp2 = temp-&gt;GetNext();
      temp-&gt;SetNext(temp2-&gt;GetNext());
      std::cout &lt;&lt; &quot;After Deletion of Node\n&quot;;
  }
  
  Node *ReverseList(Node *Head)
  {
      Node *Current, *Prev, *next;
  
      Current = Head;
      Prev = nullptr;
      while (Current != nullptr)
      {
          next = Current-&gt;GetNext();
          Current-&gt;SetNext(Prev);
          Prev = Current;
          Current = next;
      }
      Head = Prev;
      return Head;
  }
  
  int LinkedList_Count(Node *Head)
  {
      int count = 0;
  
      Node *Current = Head;
      while (Current != nullptr)
      {
          count++;
          Current = Current-&gt;GetNext();
      }
      std::cout &lt;&lt; &quot;Number of Elements: &quot; &lt;&lt; count;
      return count;
  }
  
  void PrintList(Node *Head)
  {
      std::cout &lt;&lt; &quot;Data list : &quot;;
  
      while (Head != nullptr)
      {
          std::cout &lt;&lt; &quot; &quot; &lt;&lt; Head-&gt;GetData();
          Head = Head-&gt;GetNext();
      }
      std::cout &lt;&lt; &quot;\n&quot;;
  }
  
  //Print Listed using Recursion
  void Recursion_Print(Node *Head)
  {
      if (Head == nullptr)
      {
          return;
      }
  
      std::cout &lt;&lt; ' ' &lt;&lt; Head-&gt;GetData(); //comment to Do Reverse the Linked list
      Recursion_Print(Head-&gt;GetNext());
      //std::cout &lt;&lt; ' ' &lt;&lt; Head-&gt;GetData();//unComment to Reverse the linked List recursively
  }
  
  Node *RecursiveRevList(Node *Head)
  {
      Node *temp;
      if (Head-&gt;GetNext() == nullptr)
      {
          temp = Head;
          return temp;
      }
      temp = RecursiveRevList(Head-&gt;GetNext());
      Head-&gt;GetNext()-&gt;SetNext(Head);
      Head-&gt;SetNext(nullptr);
      return temp;
  }
  void RunList()
  {
      Node *Head = NULL;
  
      //AppendList(&amp;Head, 16);
      Insert(&amp;Head, 6);
      Insert(&amp;Head, 7);
      Insert(&amp;Head, 8);
      InsertAt(&amp;Head, 18, 2);
      std::cout &lt;&lt; &quot;Data list : \n&quot;;
      Recursion_Print(Head);
      std::cout &lt;&lt; '\n';
      LinkedList_Count(Head);
      DeleteNode(&amp;Head, 1);
      //Head = ReverseList(Head);
      Head = RecursiveRevList(Head);
      PrintList(Head);
      LinkedList_Count(Head);
      delete Head;
  }
  </code></pre>