题目:

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

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

翻译:

写一个方法去删除单链表的一个节点(非尾节点),仅仅给出訪问这个节点的參数。

假定链表是1 -> 2 -> 3 -> 4,给你的节点是第三个节点(值为3),这个链表在调用你的方法后应该变为 1 -> 2 -> 4。

分析:

删除链表的节点的常规做法是改动上一个节点的next指针。然而这边并没有提供上一个节点的訪问方式。故转变思路改为删除下一节点。直接将下一个节点的val和next赋值给当前节点。

代码:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void deleteNode(ListNode node) {
node.val=node.next.val;
node.next=node.next.next;
}
}

Leet Code OJ 237. Delete Node in a Linked List [Difficulty: Easy]的更多相关文章

  1. 237. Delete Node in a Linked List【easy】

    237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...

  2. 237. Delete Node in a Linked List(C++)

    237. Delete Node in a Linked Lis t Write a function to delete a node (except the tail) in a singly l ...

  3. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

  4. [LeetCode] 237. Delete Node in a Linked List 解题思路

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

  5. [LeetCode] 237. Delete Node in a Linked List_Easy tag: Linked List

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

  6. (easy)LeetCode 237.Delete Node in a Linked List

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

  7. leetcode 237 Delete Node in a Linked List python

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

  8. 【LeetCode】237. Delete Node in a Linked List

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

  9. 17.leetcode 237. Delete Node in a Linked List

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

随机推荐

  1. git删除远程仓库文件

    1 首先将远程代码pull到本地,保持本地仓库跟远端仓库同步 git pull git@github.com:lanleilin/lanGallery.git//使用SSH的方式 2 然后使用git ...

  2. Nginx ServerName 配置说明(转)

    Nginx强大的正则表达式支持,可以使server_name的配置变得很灵活,如果你要做多用户博客,那么每个用户拥有自己的二级域名也就很容易实现了. 下面我就来说说server_name的使用吧: s ...

  3. Bzoj3652 大新闻

    Time Limit: 10 Sec  Memory Limit: 512 MBSec  Special JudgeSubmit: 215  Solved: 112 Description Input ...

  4. 【BZOJ4481&JSOI2015】非诚勿扰(数学期望)

    听说JSOI有版权问题就不放图了 如果前面的文章里的图需要删掉请通知我 题意:有一些女的要挑一些男的,挑中的几率均为p.一个男的可以无限次被挑中.若女a选中男b,女c选中男d,a<c,b> ...

  5. javascript屏蔽脏字

    原文发布时间为:2009-04-16 -- 来源于本人的百度文章 [由搬家工具导入] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...

  6. c#中类与结构的区别 struct与class的区别

    原文发布时间为:2008-11-23 -- 来源于本人的百度文章 [由搬家工具导入] 类与结构的实例比较   类与结构的差别   如何选择结构还是类   一.类与结构的示例比较:   结构示例:    ...

  7. the project was not built since its build……

    [问题描述] 用eclipse编译程序时,出现下面错误: The project was not built since its build path is incomplete. Cannot fi ...

  8. javascript 省市二级联动

    通过遍历二维数组 获取到 二级列表的 每个option 然后onchange事件 获取到省,然后循环遍历该省具有的市并将遍历到的市添加到id为city的选择器中. 获取完需要清空二级列表的内容,不然不 ...

  9. hdu1465 动态规划

    题目 一个人写了n封不同的信及相应的n个不同的信封,他把这n封信都装错了信封,问都装错信封的装法有多少种? 解体思路 用A.B.C--表示写着n位友人名字的信封,a.b.c--表示n份相应的写好的信纸 ...

  10. LeetCode OJ-- Remove Nth Node From End of List

    https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ remove倒数第n个节点 一般list remove node的 ...