Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:47:17+00:00 2026-06-15T15:47:17+00:00

node *head, *current, *temp; current = head; while(NULL != current){ temp = current; current

  • 0
node *head, *current, *temp;
current = head;
while(NULL != current){
  temp = current;
  current = current->next;
}
delete current;
current = temp;
current->next = NULL;

I’m just wondering I know there’s something like this and there’s recursion, assuming there’s already a linked list and we don’t know how many nodes are in the list.

By delete I mean deallocate.

node *head, *current, *temp;
current = head;
while(NULL != current->next){
  temp = current;
  current = current->next;
}
if (NULL != current){
delete current;
current = temp;
current->next = NULL;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:47:18+00:00Added an answer on June 15, 2026 at 3:47 pm

    Your code does not do anything: the exit condition of the loop is NULL == current, meaning that delete current; will always pass NULL to delete. This is OK, but it does not de-allocate anything.

    You should change your loop to stop when current->next == NULL, but you must be extra careful to make sure that you do not dereference current when it is equal to NULL:

    temp = 0;
    while(current && current->next){
        temp = current;
        current = current->next;
    }
    if (temp) {
        delete current;
        temp->next = 0;
    }
    

    EDIT : Your edited code mimics my answer to a large extent, except yours would fail if head == NULL. It would also fail if the list has exactly one node, because temp would remain unassigned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public boolean isPalindrome() { Stack myStack = new Stack(); for(Node current = head; current!=null;
I have this very simple C++ class: class Tree { public: Node *head; };
Been banging my head against this for a while. I cant figure out why
#include cs163hw1.h extras::extras(int num_cats){ head = new category_node; head->next = NULL; head->category = num_cats;
4 node Binary Search tree: 2143. there are 3 ways to insert and get
struct node { node(int _value, node* _next) : value(_value), next(_next) {} int value; node*
struct Node { int value Node* next; } typedef List Node* const Set operator
Can node.js be setup to recognize a proxy (like Fiddler for example) and route
I have a class like this: template <class T> class bag { private: typedef
I have been trying to implement the delete BST function but I don't know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.