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.

法一:思路是:从node 节点开始,把后面节点的值赋给前面的节点,直到到表尾;

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

法二:将要删除节点后一个节点的值赋给要删除的节点,然后删除后一个节点。

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

[Leetcode]237. Delete Node in a Linked List -David_Lin的更多相关文章

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

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

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

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

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

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

  7. LeetCode 237. Delete Node in a Linked List 删除链表结点(只给定要删除的结点) C++/Java

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

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

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

随机推荐

  1. [转]HTML5 script 标签的 crossorigin 属性到底有什么用?

    HTML5 script 标签的 crossorigin 属性到底有什么用? 最近Bootstrap 4已经正式发布了,可能已经有爱尝鲜的小伙伴在 alpha 阶段就尝试过 BS4.不过今天要说的不是 ...

  2. Fiddler 过滤设置

    1.User Fiters启用     2.Action Action:Run Filterset now是否运行,Load Filterset加载,Save Filterset保存:   3.Hos ...

  3. 登录RabbitMQ的方法

    一:(运行RabbitMQ之前需要先打开docker 容器)打开相应的路径,在windows Powershell 管理员下打开 输入:docker-compose -f .\docker-compo ...

  4. ASP.NET C# 实现实时用户在线

    public static class UserOnline { /// <summary> /// 获取或设置在线列表 /// </summary> public stati ...

  5. 机器学习(六)K-means聚类、密度聚类、层次聚类、谱聚类

    本文主要简述聚类算法族.聚类算法与前面文章的算法不同,它们属于非监督学习. 1.K-means聚类 记k个簇中心,为\(\mu_{1}\),\(\mu_{2}\),...,\(\mu_{k}\),每个 ...

  6. curl命令行请求

    curl -H "Content-Type: application/json" -X POST --data 'json post数据' -i http://xxx

  7. Flutter 读写本地文件

    文档 注意 安装 path_provider 插件后重启f5, 而不是等待热更新 demo import 'dart:io'; import 'dart:async'; import 'package ...

  8. 【ASP】session实现购物车

    1.问题提出 利用session内置对象,设计并实现一个简易的购物车,要求如下: 1)利用用户名和密码,登录进入购物车首页 2)购物首页显示登录的用户名以及该用户是第几位访客.(同一用户的刷新应该记录 ...

  9. SPARK-AM-TrackURL-UI-500

    HTTP ERROR 500 Problem accessing /proxy/application_1538120222810_0072/. Reason: Connection refused ...

  10. koa2学习(一)

    前期准备: node环境 npm包管理工具 安装Koa npm install --save koa 第一个程序 创建index.js const Koa = require('koa'); cons ...