码迷,mamicode.com
首页 > 其他好文 > 详细

CC150 2.3

时间:2014-11-24 08:45:39      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:interview

2.3 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


It seems cannot direclty delete the node.

But we can delete the data.

void delete(Node toDelete)
{
  if (toDelete == null)
    return null;
    
  Node n = toDelete;
  while (n!= null)
  {
    if (n.next != null)
    {
      n.data = n.next.data;
      if (n.next.next == null)
      {
        n.next = null;
      }
    }
    n= n.next;
  }
}


CC150 2.3

标签:interview

原文地址:http://7371901.blog.51cto.com/7361901/1581731

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!