问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3832 访问。

请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。

现有一个链表 -- head = [4,5,1,9],它可以表示为:

4 -> 5 -> 1 -> 9

输入: head = [4,5,1,9], node = 5

输出: [4,1,9]

解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.

输入: head = [4,5,1,9], node = 1

输出: [4,5,9]

解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.

说明:

链表至少包含两个节点。

链表中所有节点的值都是唯一的。

给定的节点为非末尾节点并且一定是链表中的一个有效节点。

不要从你的函数中返回任何结果。


Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Given linked list -- head = [4,5,1,9], which looks like following:

4 -> 5 -> 1 -> 9

Input: head = [4,5,1,9], node = 5

Output: [4,1,9]

Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.

Input: head = [4,5,1,9], node = 1

Output: [4,5,9]

Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.

Note:

The linked list will have at least two elements.

All of the nodes' values will be unique.

The given node will not be the tail and it will always be a valid node of the linked list.

Do not return anything from your function.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3832 访问。

public class Program {

    public static void Main(string[] args) {
var head = new ListNode(1) {
next = new ListNode(2) {
next = new ListNode(3) {
next = new ListNode(4) {
next = new ListNode(5)
}
}
}
}; DeleteNode(head.next.next);
ShowArray(head); Console.ReadKey();
} private static void ShowArray(ListNode list) {
var node = list;
while(node != null) {
Console.Write($"{node.val} ");
node = node.next;
}
Console.WriteLine();
} private static void DeleteNode(ListNode node) {
//先复制下一个节点的值
node.val = node.next.val;
//再把下一节点的指针指向下下一个节点
node.next = node.next.next;
} public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3832 访问。

1 2 4 5

分析:

显而易见,以上算法的时间复杂度为: 

最新文章

  1. 关于MVC的开源商城 Nop之闲聊
  2. Golang友团无闻Go语言Web基础视频教程
  3. 创建一个自定义颜色IRgbColor
  4. [WPF]TextTrimming截断后,ToolTip显示完整信息
  5. linux下subversion server安装手册
  6. 将AJAX返回值纵向排序赋值给Table标签
  7. RedHat5配置网卡
  8. decimal to hexadecimal,binary and octonary.
  9. Microsoft Visual Studio 6.0 Enterprise Edition
  10. mac 显示隐藏文件方法
  11. 您可能无法使用服务器管理器,如果两个线程同时访问 IIS 管理 IIS 的修补程序
  12. 斗牛app上架应用宝、牛牛手机游戏推广、百人牛牛app应用开发、棋牌游戏上传、手游APP优化
  13. kubectl 常用命令总结
  14. 在iframe窗体内 获取父级的元素;;在父窗口中获取iframe中的元素
  15. python enumerate用法总结--转载
  16. JavaScript数组中的22个常用方法
  17. P4331 [BOI2004]Sequence 数字序列 (左偏树)
  18. jq 侧边栏
  19. 百度地图 JS API开发Demo01
  20. 关于ArcGis for javascrept之Map类

热门文章

  1. 如何写一个自己的HashMap
  2. CentOS8.0 Docker Repository
  3. less : 解决升级后报错的问题
  4. 高效C++:定制new和delete
  5. 题解 洛谷 P5465 【[PKUSC2018]星际穿越】
  6. 004.Nginx日志配置及状态监控
  7. APP自动化 -- MobileBy
  8. windows系统远程修改密码
  9. ORACLE_19c用户密码登录失败的问题以及ORA-28040
  10. MySQL组复制MGR(二)-- 组复制搭建