★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-dlyqvani-mc.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.node
Given linked list -- head = [4,5,1,9], which looks like following:git
Example 1:github
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.
Example 2:微信
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:函数
请编写一个函数,使其能够删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。spa
现有一个链表 -- head = [4,5,1,9],它能够表示为:code
4 -> 5 -> 1 -> 9
示例 1:htm
输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数以后,该链表应变为 4 -> 1 -> 9.
示例 2:blog
输入: head = [4,5,1,9], node = 1 输出: [4,5,9] 解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数以后,该链表应变为 4 -> 5 -> 9.
说明:
0ms
1 class Solution { 2 public void deleteNode(ListNode node) { 3 node.val=node.next.val; 4 node.next=node.next.next; 5 } 6 }
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 class Solution { 10 public void deleteNode(ListNode node) { 11 12 int temp; 13 temp = node.val; 14 node.val = node.next.val; 15 node.next.val = temp; 16 17 node.next = node.next.next; 18 19 } 20 }