Ch2-3: remove the middle node in a singly linked list

来源:互联网 发布:2017女生标准体重算法 编辑:程序博客网 时间:2024/05/05 09:43

Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node.
EXAMPLE
Input: the node ‘c’ from the linked list a->b->c->d->e Result: nothing is returned, but the new linked list looks like a->b->d->e

Solution: 

Since only the removed node of the singly linked list is given, we can only find all the next but not going back. So we can copy the next node of this removable node so as to "remove" it.

One thing should notice is the corner condition:

1. head   2. middle   3. last one

for 1,2 is fine. For 3, we cannot use this method so can't solve it.

Here is the fully code:


Output: 

Executing the program....$demo 10 9 8 7 6 5 4 3 2 1 10 9 7 6 5 4 3 2 1 


0 0