Delete a node in a single link list that takes O(1) time

H

hotice

How to write code to delete a specific node in a single link list that
takes O(1) time? £¨ link list uses pointers, not hash. £© That is, the
time deleting a node is the same (independent from the length of the
list. Show your c/c++ source code.
 
I

Ian Collins

hotice said:
How to write code to delete a specific node in a single link list that
takes O(1) time? £¨ link list uses pointers, not hash. £© That is, the
time deleting a node is the same (independent from the length of the
list. Show your c/c++ source code.
You first!
 
D

Daniel T.

hotice said:
How to write code to delete a specific node in a single link list that
takes O(1) time? £® link list uses pointers, not hash. £© That is, the
time deleting a node is the same (independent from the length of the
list. Show your c/c++ source code.

Think about how a single linked list works. What is a node in such a
list? What properties does it have? Now think of a couple ways to remove
a node from the list. What information do you need to do it? Do
different removal strategies require different information?

Do all the above without code first. Write a simple description of what
needs to happen in what ever language you feel most comfortable in.
 
S

Salt_Peter

How to write code to delete a specific node in a single link list that
takes O(1) time? £¨ link list uses pointers, not hash. £© That is, the
time deleting a node is the same (independent from the length of the
list. Show your c/c++ source code.

What you describe is not a linked list anymore.
Show your own code.
 
A

Alf P. Steinbach

* Salt_Peter:
What you describe is not a linked list anymore.

Well, not that I like doing homework for anyone, but this problem goes
back to about 1967. It's well known with well known solution. Young
"hotice" is just too dishonest to think of any way of finding a solution
other than to cheat, not even doing his own googling.

Show your own code.

Second that.
 
C

Clark Cox

What you describe is not a linked list anymore.

Of course it is. In fact, I'd say that the obvious way of removing an
element from a linked list is O(1) time. (think about it).
 
S

Salt_Peter

Of course it is. In fact, I'd say that the obvious way of removing an
element from a linked list is O(1) time. (think about it).

I agree with the O(1), but thats not was he's asking for.
 
S

Salt_Peter

Of course it is. In fact, I'd say that the obvious way of removing an
element from a linked list is O(1) time. (think about it).

Its definitely O(1), he doesn't know it but thats not what he's asking
for.
 
J

Juha Nieminen

hotice said:
How to write code to delete a specific node in a single link list that
takes O(1) time? £¨ link list uses pointers, not hash. £©

What info do you have to delete the node? If you have only a pointer
to the node to be deleted and nothing else, it's obviously impossible
to delete it in constant time. You need a pointer to the previous node.
 
A

Alf P. Steinbach

* Juha Nieminen:
What info do you have to delete the node? If you have only a pointer
to the node to be deleted and nothing else, it's obviously impossible
to delete it in constant time. You need a pointer to the previous node.

No.
 
H

Howard

---------
list_t *tmp = this->next;
this->val = this->next->val;
this->next = this->next->next;
delete tmp;

Consider: what if you're at the end of the list, and this->next is NULL?

-Howard

(P.S., please don't do other people's homework for them. How are they
supposed to learn anything if we help them cheat on their assignments?)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top