题目

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.

分析

题目要求删除指定结点,但是给定参数只有目标结点。

转移思维,改成拷贝目标结点后结点的值,然后删除目标结点的后一结点。

AC代码

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
if (node == NULL)
return;
else if(node->next != NULL){
//只给出一个要删除的结点,转移思维,改成拷贝目标结点后结点的值,然后删除目标结点的后一结点
node->val = node->next->val;
node->next = node->next->next;
}
else{
return;
}
}
};

GitHub 测试程序源码

LeetCode(237)Delete Node in a Linked List的更多相关文章

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

  2. [LC]237题 Delete Node in a Linked List (删除链表中的节点)(链表)

    ①中文题目 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 示例 1: 输入: hea ...

  3. LeetCode(114) Flatten Binary Tree to Linked List

    题目 分析 按要求转换二叉树: 分析转换要求,发现,新的二叉树是按照原二叉树的先序遍历结果构造的单支二叉树(只有右子树). 发现规则,便容易处理了.得到先序遍历,构造即可. AC代码 /** * De ...

  4. 237. Delete Node in a Linked List(C++)

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

  5. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  6. [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 Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

  8. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  9. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

随机推荐

  1. iOS NavigationBar 导航栏自定义

    1. 设置导航栏NavigationBar的背景颜色: a)  setBarTintColor : 设置NagivationBar的颜色 也可以用 : [[UINavigationBar appear ...

  2. 1-2JDK的安装和环境变量设置

    JDK的下载和安装 可以去oracle官网里面下载JDK:http://www.oracle.com 英文不好的同学可以通过下面这个百度网盘的链接进行下载:http://pan.baidu.com/s ...

  3. OGG How to Resync Tables / Schemas on Different SCN s in a Single Replicat

    To resync one or more tables/schemas on different SCN's using a single or minimum number of replicat ...

  4. 迭代器模式及php实现

    迭代器模式: 迭代器模式是遍历集合的成熟模式,迭代器模式的关键是将遍历集合的任务交给一个叫做迭代器的对象,它的工作时遍历并选择序列中的对象,而客户端程序员不必知道或关心该集合序列底层的结构. 角色: ...

  5. 【学习笔记】深入理解js原型和闭包(13)——【作用域】和【上下文环境】

    上文简单介绍了作用域,本文把作用域和上下文环境结合起来说一下,会理解的更深一些. 如上图,我们在上文中已经介绍了,除了全局作用域之外,每个函数都会创建自己的作用域,作用域在函数定义时就已经确定了.而不 ...

  6. Android Activity生命周期的几个问题

      每一个Android开发者都应该知道,android系统有四个重要的基本组件,即Activity(活动).Service(服务).Broadcast Receive(广播接收器)和Content ...

  7. 在项目中运用精益 - Five Why

    今天成功地运用了5个why对一个项目上的问题进行剖析,收获良多,在此分享给大家. 事情是这样的,该项目是个固定价格项目,处于收尾阶段,客户提出来了一些需求,我们(实施方)认为这些要求是不在最初项目范围 ...

  8. 字符串循环右移-c语言

    一个长度为len的字符串,对其循环右移n位 [期望]char str[] = "abcdefg";右移3次后,变成"efgabcd" [思路] 思路1. 如果用 ...

  9. JavaScript Html2canvas 生成高清图片(移动端模糊问题)

    最近在做一个移动端的项目,简单记录一下该功能. 需求是这样的: 将带有二维码和一些介绍信息 动态生成一张图片 比如说是 生成这样的图片,文字.主图.价格.二维码都是不固定的. 对于这个需求,看见微信上 ...

  10. df - 报告文件系统磁盘空间的使用情况

    总览 df [OPTION]... [FILE]... POSIX 选项: [-kP] GNU 选项 (最短方式): [-ahHiklmPv] [-t fstype] [-x fstype] [--b ...