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.
分析
题目要求删除指定结点,但是给定参数只有目标结点。 
转移思维,改成拷贝目标结点后结点的值,然后删除目标结点的后一结点。
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;
        }
    }
};
LeetCode(237)Delete Node in a Linked List的更多相关文章
- 【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 ... 
- [LC]237题 Delete Node in a Linked List (删除链表中的节点)(链表)
		①中文题目 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 示例 1: 输入: hea ... 
- LeetCode(114) Flatten Binary Tree to Linked List
		题目 分析 按要求转换二叉树: 分析转换要求,发现,新的二叉树是按照原二叉树的先序遍历结果构造的单支二叉树(只有右子树). 发现规则,便容易处理了.得到先序遍历,构造即可. AC代码 /** * De ... 
- 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 ... 
- LeetCode(116) Populating Next Right Pointers in Each Node
		题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ... 
- [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 ... 
- 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 ... 
- 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 ... 
- 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 ... 
随机推荐
- Linux Export命令
			一.Windows 环境变量 1.在Windows 系统下,很多软件安装都需要配置环境变量,比如 安装 jdk ,如果不配置环境变量,在非软件安装的目录下运行javac 命令,将会报告找不到文件,类似 ... 
- CGI和Servlet的比较
			转载自:http://www.maxhis.info/java/cgi-vs-servlet/ 概括来说,CGI和Servlet可以完成相同的功能. CGI(Common Gateway Interf ... 
- Spark MLlib编程API入门系列之特征提取之主成分分析(PCA)
			不多说,直接上干货! 主成分分析(Principal Component Analysis,PCA), 将多个变量通过线性变换以选出较少个数重要变量的一种多元统计分析方法. 参考 http://blo ... 
- 分享几个自己喜欢的前端UI框架
			http://www.layui.com/ http://element-cn.eleme.io/#/zh-CN/component/installation 
- 页面html图片按钮多种写法
			原地址:http://blog.163.com/weison_hi/blog/static/17680404720118534033788/ 第一种: 在一般情况下按钮提交表单: <form i ... 
- 使用gitblit 在windows平台搭建git服务器
			1.下载jdk,安装并且配置好环境变量 2.下载gitblit 直接解压无需安装 3.配置gitblit 1.修改gitblit安装目录下的data文件下的gitblit.properties.将in ... 
- Xilinx HLS
			Xilinx 的高层次综合(High Level Synthesis, HLS)技术是将C/C++/SystemC软件语言转换成Verilog或VHDL硬件描述语言的技术.现已应用在SDAccel,S ... 
- 编译安装LAMP之php(fpm模块)
			一,准备工作实验平台为CentOS6.6,先下载所需的安装包,我使用的是php-5.4.26.tar.gz,下载地址 http://mirrors.sohu.com/php/ 编译安装的目录:/usr ... 
- Struts2控制文件的上传与下载
			Struts2控制文件上传与下载的几个注意事项: (1)必须将表单的method设置为post,将enctype设置为multipart/from-data.只有这样,浏览器才会把用户选择文件的二进制 ... 
- Redis学习笔记(五)散列进阶
			HEXISTS key_name key(检查键key是否存在) HKEYS key_name(获得散列的所有键) HVALS key_name(获得散列的所有值) HINCRBY key_name ... 
