ITeye博客

How to delete a node in a LinkList ?

standalone · 2010-07-06 21:22 · algorithm · 编程语言 · Markdown

If only given the pointer to the node to be deleted? Note the node is not the first one nor the last.

 

如果在单链表中删除一个指定的结点? 需要注意的是不是头尾结点。

 

这到题其实就是个小技巧,没有告诉前面的一个结点怎么做删除呢?答案就是把后面结点的内容拷贝过来!这个问题的前提是结点不能使首尾结点,否则就没法做了。

 

假设数据内容是整型,简化问题。

 

void delete(Node p){
   Node q = p.next;
   p.data = q.data;
   p.next = q.next;
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。