leetcode笔记--Delete Node in a Linked List
2016-02-19 14:36
591 查看
题目:难度(Easy)
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.
Tags:Linked List
Similar Problems:(E)Remove Linked List Elements
分析:要删除一个节点,我们需要知道这个节点的前驱节点,而输入给的是要删除的节点的指针,我们并不能获得他的前驱,这就需要使用一个小小的技巧。假设当前要删除的节点的指针是p,我们可以将p.next的元素值复制到p,然后删除p.next节点就行了。
代码实现:
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.
Tags:Linked List
Similar Problems:(E)Remove Linked List Elements
分析:要删除一个节点,我们需要知道这个节点的前驱节点,而输入给的是要删除的节点的指针,我们并不能获得他的前驱,这就需要使用一个小小的技巧。假设当前要删除的节点的指针是p,我们可以将p.next的元素值复制到p,然后删除p.next节点就行了。
代码实现:
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def deleteNode(self, node): """ :type node: ListNode :rtype: void Do not return anything, modify node in-place instead. """ p = node q = node.next p.val = q.val p.next = q.next
相关文章推荐
- node.js学习笔记之babel使用
- NodeJS中使用swig模板引擎
- node学习之第一个接口,简单的登录验证
- leetcode笔记--Remove Nth Node From End of List
- LeetCode - 25. Reverse Nodes in k-Group
- Node.js
- LeetCode - 24. Swap Nodes in Pairs
- namenode fsimage edits
- node查询mySql表
- windows下node.js环境配置
- [Nodejs] 记录一个小坑:Can't set headers after they are sent
- node递归属性目录结构
- node递归属性目录结构
- 我们为什么要看《超实用的Node.JS代码段》
- 五子棋(围棋) canvas node html5 js
- 我们为什么要看《超实用的Node.JS代码段》
- hadoop+zookepper实现namenode的高可用
- NodeJS 常用模块
- Node.JS 学习路线图
- node处理get请求