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

思路,之前就看过这种怎么删除链表的一个节点,就是不利用外来引用也不需要遍历的情况下。但是这个算法就要求不能是尾节点。

public void deleteNode(ListNode node) {
        if (node == null || node.next == null)
            return;
        node.val = node.next.val;
        node.next = node.next.next;
    }
时间: 2024-10-03 02:01:02

[LeetCode]--237. Delete Node in a Linked List的相关文章

LeetCode 237 Delete Node in a Linked List(在链表中删除节点)

翻译 给定一个访问节点的路径,写一个函数去删除在一个单向链表中除尾部以外的节点. 假设这个链表是1 -> 2 -> 3 -> 4,并且你被给予了第3个值为3的节点,那么在调用你的函数之后这个链表应该变为1 -> 2 -> 4. 原文 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the l

[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 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 ->

[LeetCode] Second Minimum Node In a Binary Tree 二叉树中第二小的结点

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes.

[LeetCode]114.Flatten Binary Tree to Linked List

[题目] Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 [分析] 在先序遍历的过程中把二叉树转为链表. 用pre记住当前节点的前一节点.节点的右指针作为链表指针,同时左指针赋空(第一次wrong就是因为没赋空). pre->ri

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

python列表操作实例_python

本文实例讲述了python列表操作的方法.分享给大家供大家参考. 具体实现方法如下: 复制代码 代码如下: class Node:    """Single node in a data structure"""      def __init__(self, data):       """Node constructor"""              self._data = da

深入浅出PageRank算法

PageRank算法 PageRank算法是谷歌曾经独步天下的"倚天剑",该算法由Larry Page和http://www.aliyun.com/zixun/aggregation/16959.html">Sergey Brin在斯坦福大学读研时发明的,论文点击下载: The PageRank Citation Ranking: Bringing Order to the Web. 本文首先通过一些参考文献引出问题,然后给出了PageRank的几种实现算法,最后将其推

有关java的白痴问题,希望大神回答

问题描述 有关java的白痴问题,希望大神回答 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 shoul

CareerCup之2.1无序链表删除重复元素

[题目] 原文: 2.1 Write code to remove duplicates from an unsorted linked list. FOLLOW UP How would you solve this problem if a temporary buffer is not allowed? 译文: 从一个未排序的链表中移除重复的项 进一步地, 如果不允许使用临时的缓存,你如何解决这个问题? [分析] (1)如果可以使用额外的存储空间,我们就开一个数组来保存一个元素的出现情况.