题目

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代码

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. void deleteNode(ListNode* node) {
  12. if (node == NULL)
  13. return;
  14. else if(node->next != NULL){
  15. //只给出一个要删除的结点,转移思维,改成拷贝目标结点后结点的值,然后删除目标结点的后一结点
  16. node->val = node->next->val;
  17. node->next = node->next->next;
  18. }
  19. else{
  20. return;
  21. }
  22. }
  23. };

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 将WKWebView内的HTML打印为PDF

    使用的webview为WKWebView,核心部分代码(Swift 4): // 创建打印渲染 let printPageRenderer:PDFRender = PDFRender() // 获取渲 ...

  2. Robot Framework操作mongodb数据库

    RF对mongodb操作需要安装以下两个库: 1.pymongo 可以采用pip install pymongo: (如果下载过慢,可指定下载源,如:http:pypi.douban.com/simp ...

  3. Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) M

    Description The marmots have prepared a very easy problem for this year's HC2 – this one. It involve ...

  4. 通过表单展示不一样的页面(input对象)

    表单中包含不一样的样式,不同功能的提交数据的方式.在许多页面中,浏览者不经意间已经不断在使用表单的功能,如留言,设置自己的密码或者是复选框,下拉列表等. input对象下的多种表单表现形式: 通常在页 ...

  5. HDU 1223 打表 + 大数

    http://acm.hdu.edu.cn/showproblem.php?pid=1223 一般遇到这些题,我都是暴力输出前几项,找规律.未果. 然后输出n = 1时候,以A开始,有多少个答案, n ...

  6. Node.Js的Module System 以及一些常用 Module

    Node.Js学习就按照这本书的流程来. 在第7章结束与第10章结束时分别自己出一个小项目练练手.Node.Js的入门学习计划是这样. 目录:, QQ:1045642972 欢迎来索书以及讨论Node ...

  7. 第一个 swift 项目

    今天 学习了 一丢丢 swift,特此记录一下 ! 原来创建的时候 ,只要把 语言 由以前的Object-C改为Swift,变创建好了自己的swift工程 第一个简单的swift demo 上代码 i ...

  8. js单线程和js异步操作的几种方法

    一.为什么JavaScript是单线程? JavaScript语言的一大特点就是单线程,也就是说,同一个时间只能做一件事. JavaScript的单线程,与它的用途有关.作为浏览器脚本语言,JavaS ...

  9. 原创:E325: ATTENTION vim超完整超给力的问题与解决方法

    又到了老葵花哥哥开课的时间 这是给大家提供一个企业常见的错误 我相信大家生活还编程中会长期使用接触这个错误 这里我们经常用的两个选项 (E)dit any way 编辑原来的文件,忽略刚刚做的修改 ( ...

  10. java生成饼图svg

    package com.tellhow.svg; import java.io.File;import java.io.FileOutputStream; /** *  * @author 风絮NO. ...