title: How to delete the shortest word from the linked list without array? C/C++
body: <p>How can I delete the shortest word from a linked list without array? I am a first course student and learning pointers with linked list etc.
  This is my last task to do and I'm stuck.</p>
  <p>This code finds the shortest word of the list and I need to delete that word. I don't know how to delete that specific word.</p>
  <p>My problem is that the <code>del</code> function deletes wrong word:</p>
  <pre><code>void del (Node *shorter)
  {
      Node* temp;
      temp = shorter-&gt;next;
      shorter-&gt;next = temp-&gt;next;
      cout&lt;&lt;&quot;Deleted: &quot;&lt;&lt;temp-&gt;data&lt;&lt;endl;
      delete temp;
  }
  </code></pre>
  <p>This is my full code:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;iterator&gt;
  
  using namespace std;
  
  std::vector&lt;std::string&gt; split(const std::string&amp; source, const std::string&amp; delimiters = &quot; &quot;) {
      std::size_t prev = 0;
      std::size_t currentPos = 0;
      std::vector&lt;std::string&gt; results;
  
      while ((currentPos = source.find_first_of(delimiters, prev)) != std::string::npos) {
          if (currentPos &gt; prev) {
              results.push_back(source.substr(prev, currentPos - prev));
          }
          prev = currentPos + 1;
      }
      if (prev &lt; source.length()) {
          results.push_back(source.substr(prev));
      }
      return results;
  }
  
  struct Node {
      std::string data;
      Node* next;
  };
  struct Node* head = NULL;
  
  Node* createList() {
      string text;
      cout &lt;&lt; &quot;Write text: &quot;;
      getline(cin, text);
  
      Node *head = new Node();
      head-&gt;next = NULL;
      Node *current = head;
  
      string delimiters = &quot; ,.-':;?()+*/%$#!\&quot;@^&amp;&quot;;
      auto results = split(text, delimiters);
  
      bool isFirst = true;
      for (const auto&amp; word : results) {
          if (isFirst) {
              current-&gt;data = word;
              isFirst = false;
          } else {
              Node *newNode = new Node();
              newNode-&gt;data = word;
              current-&gt;next = newNode;
              current = newNode;
          }
      }
  
      return head;
  }
  
  void del (Node *shorter)
  {
      Node* temp;
      temp = shorter-&gt;next;
      shorter-&gt;next = temp-&gt;next;
      cout&lt;&lt;&quot;Deleted: &quot;&lt;&lt;temp-&gt;data&lt;&lt;endl;
      delete temp;
  }
  
  
  void findShortestWord(Node* head) {
      Node *current = head;
      Node *shorter = head;
  
      while (current != NULL) {
          if (current-&gt;data.size() &lt; shorter-&gt;data.size()) {
              shorter-&gt;data = current-&gt;data;
          } else if (current-&gt;data.size() &gt; shorter-&gt;data.size()) {
              current = current-&gt;next;
          } else if (current-&gt;data.size() == shorter-&gt;data.size()) {
              current = current-&gt;next;
          }
      }
      cout &lt;&lt; &quot;_____________________________________________________________&quot; &lt;&lt; endl;
      cout &lt;&lt; &quot;Shortest word: &quot; &lt;&lt; shorter-&gt;data &lt;&lt; &quot;                                |&quot; &lt;&lt;endl;
      cout &lt;&lt; &quot;_____________________________________________________________|&quot; &lt;&lt; endl;
      del(shorter);
  }
  
  void print(Node* head)
  {
      if (head == NULL and cout &lt;&lt; endl)
          return;
      cout&lt;&lt;&quot;\nThe word you entered: &quot;;
      cout &lt;&lt; head-&gt;data &lt;&lt; ' ';
      print(head-&gt;next);
  }
  
  int main() {
  
       Node *head = createList();
       print(head);
       findShortestWord(head);
       print(head);
  
      return 0;
  }
  </code></pre>