本文是在学习中的总结,欢迎转载但请注明出处: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. 信用卡3D验证相关资料

    3D 验证服务,是银行与VISA .MASTERCARD国际组织联合推出的为保障银行维萨及万事达信用卡持卡客户网上交易安全,防范网上伪冒交易的一项信用卡网上支付安全验证服务( 维萨卡使用的验证服务叫& ...

  2. Java 单元测试 JUnit4 快速入门

    JUnit最佳实践 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class CardServiceTest {     /**      * 最佳 ...

  3. Ant简介

    Ant,apache开源项目,基于Java的构建工具,是一个小程序.它通过自动完成所有的编译代码,运行测试以及 打包重新部署等繁琐费力的任务来帮助软件团队开发大程序: Ant的目标是自动完成所有的构建 ...

  4. Servlet之Request对象

    下面的方法可用在 Servlet 程序中读取 HTTP 头.这些方法通过HttpServletRequest 对象可用. 1    Cookie[] getCookies() 返回一个数组,包含客户端 ...

  5. Dynamics CRM 检测访问CRM延迟及带宽的工具

    直接在浏览器中访问如下地址"http://CRMHOST/organization/tools/diagnostics/diag.aspx"(这里的CRMHOST和organiza ...

  6. 【编程练习】最近准备开始找工作,这篇文章作为一个code练手题目的总结吧

    找工作时候一般需要准备的算法题目类型,其实参考leetcode和poj或者剑指offer基本能够摆平大部分的题目了 1.图的遍历,BFS.DFS: 2.递归的回溯剪枝: 3.树的建立和遍历: 4.状态 ...

  7. Android实现横屏以及全屏的小技巧

    分享两个安卓的实用小技巧,那就是横屏和全屏的实现. 首先是横屏的实现 首先是在清单文件中实现 <activity android:name=".MainActivity" a ...

  8. Swift基础之闭包Closure学习

    首先Swift语言中没有了Block内容,但是你可以通过调用OC文件使用,也可以使用Closure(闭包),实现Block或者Delegae同样反向传值或回调函数的效果,也可以解决函数指针的问题,两者 ...

  9. Android程序崩溃异常收集框架

    最近在写Android程序崩溃异常处理,完成之后,稍加封装与大家分享. 我的思路是这样的,在程序崩溃之后,将异常信息保存到一个日志文件中,然后对该文件进行处理,比如发送到邮箱,或发送到服务器. 所以, ...

  10. FORM中的MOAC控制

    1.创建表时,对_ALL表创建同义词 -- Create Multi Org Synonym CREATE  OR REPLACE  SYNONYM CUX_WF_DEF_HEADER FOR CUX ...