本文是在学习中的总结,欢迎转载但请注明出处: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. 一道有趣的Twitter技术面试题

    来自:http://blog.jobbole.com/50705/ 看下面这个图片” “在这个图片里我们有不同高度的墙.这个图片由一个整数数组所代表,数组中每个数是墙的高度.上边的图可以表示为数组[2 ...

  2. Java异常处理-----自定义异常

    自定义异常 问题:现实中会出现新的病,就需要新的描述. 分析: java的面向对象思想将程序中出现的特有问题进行封装. 案例: 定义功能模拟凌波登录.(例如:lb(String ip))需要接收ip地 ...

  3. 获取客户信息SQL

    /*取客户信息SQL*/ --客户信息 SELECT hou.name 业务实体, hca.account_number 客户编号, hp.party_name 客户名称, arp_addr_pkg. ...

  4. RxJava(六) retryWhen操作符实现错误重试机制

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51539708 本文出自:[余志强的博客] 业务需求 当我们在app里 ...

  5. Hadoop介绍

    是适合大数据的分布式存储与计算平台,用java编写的开源系统,能够安排在大规模的计算机平台上,从而长进计算效率:由 HDFS.MapReduce.HBase.Hive 和 ZooKeeper等成员组成 ...

  6. java创建对象详解和多态问题

    一. java 构造方法不等于创建对象而是初始化对象,new 关键字分配内存和创建对象的.  二.Test test = new Test(); 有人用上面的表达式来说明构造方法返回对象引用,这是明显 ...

  7. 20 ViewPager总结

    V4:兼容到1.6,V7:兼容到 2.1,V13:兼容到3.2(平板) 兼容包 ViewPager 父类: android.support.v4.view.ViewPager ViewPager:滑动 ...

  8. [ExtJS5学习笔记]第二十七节 CMD打包错误 Error C2009: YUI Parse Error (identifier is a reserved word => debugger;)

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/41242993 本文作者:sushengmiyan ------------------ ...

  9. javascript之事件处理

    一般事件 onclick                       鼠标点击时触发此事件 ondblclick                  鼠标双击时触发此事件 onmousedown    ...

  10. Nginx模块之SessionSticky

    0 工作原理 Session Sticky 模块在upstream 返回响应后,向客户的浏览器写入 Cookie ,默认名为route ,保存的内容是一个 md5 码. 之后,模块接收到客户浏览器的请 ...