本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/47334649

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)该题比较简单。详情见下方代码。希望本文对你有所帮助。

算法代码实现如下:

package leetcode;

import leetcode.utils.ListNode;

/**
 *
 * @author lqq
 *
 */
public class Delete_Node_in_a_LinkedList {

	public void deleteNode(ListNode node) {
		if (node == null)
			return;

		ListNode after = node.next;
		if (after != null) {
			int value = after.val;
			node.val = value;
			node.next = after.next;
		}
	}
}

Leetcode_237_Delete Node in a Linked List的更多相关文章

  1. [LeetCode] 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 ...

  2. Leetcode 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 ...

  3. 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 ...

  4. 【4_237】Delete Node in a Linked List

    Delete Node in a Linked List Total Accepted: 48121 Total Submissions: 109297 Difficulty: Easy Write ...

  5. Leetcode-237 Delete Node in a Linked List

    #237.    Delete Node in a Linked List Write a function to delete a node (except the tail) in a singl ...

  6. [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点

    2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...

  7. (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 ...

  8. leetcode: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 ...

  9. 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements

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

随机推荐

  1. 使用Contacts Contract Content Provider操作通讯录最佳实践

    Android向所有被赋予READ_CONTACTS权限的应用程序提供了联系人信息数据库的完全访问权限.Contacts Contract使用3层数据模型去存储数据,下面介绍Contacts Cont ...

  2. Pycharm中进行Python远程开发

    http://blog.csdn.net/pipisorry/article/details/52269952 PyCharm提供两种远程调试(Remote Debugging)的方式:    配置远 ...

  3. JAVA面向对象-----接口与类、接口之间的关系

    接口与类.接口之间的关系 大家之前都知道类与类之间的关系继承,那么接口与类之间又是怎样子的关系呢? 接口与类之间是实现关系.非抽象类实现接口时,必须把接口里面的所有方法实现.类实现接口用关键字impl ...

  4. Maven插件详解

    插件与插件目标 Maven定义了三套相互独立的生命周期,每套生命周期都有多个生命周期阶段,而这些阶段都是抽象的,不做任何工作.真正完成工作的是绑定在生命周期阶段的插件目标.插件以独立的构件形式存在,一 ...

  5. ORACLE数据库学习之逻辑结构

     逻辑结构 数据库逻辑结构包含表空间.段.范围(extent).数据块和模式对象. (一)表空间 一个数据库划分为一个或多个逻辑单位,该逻辑单位称为表空间类似于sybase下的设备.(TABLES ...

  6. 多线程之Java线程阻塞与唤醒

    线程的阻塞和唤醒在多线程并发过程中是一个关键点,当线程数量达到很大的数量级时,并发可能带来很多隐蔽的问题.如何正确暂停一个线程,暂停后又如何在一个要求的时间点恢复,这些都需要仔细考虑的细节.在Java ...

  7. Mybatis源码分析之存储过程调用

    这一篇博客我们学习一下Mybatis调用存储过程的使用和运行流程.首先我们先创建一个简单的存储过程 DELIMITER $ CREATE PROCEDURE mybatis.ges_user_coun ...

  8. javascript命名规则

    javascript对大小写敏感(关键字.函数名.变量名等),标识符的首字符必须是字母.下划线或者$符,其后的字符可以含数字 如果之声明了变量,并未对其赋值,默认为undefined javascri ...

  9. Servlet之Session处理

    HttpSession 对象中可用的几个重要的方法: 1    public Object getAttribute(String name) 该方法返回在该 session 会话中具有指定名称的对象 ...

  10. UNIX网络编程——客户/服务器程序设计示范(三)

    TCP预先派生子进程服务器程序,accept无上锁保护 我们的第一个"增强"型服务器程序使用称为预先派生子进程的技术.使用该技术的服务器不像传统意义的并发服务器那样为每个客户现场派 ...