LeetCode_237. Delete Node in a Linked List
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.
Given linked list -- head = [4,5,1,9], which looks like following:

Example 1:
Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
Example 2:
Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
Note:
- The linked list will have at least two elements.
- All of the nodes' values will be unique.
- The given node will not be the tail and it will always be a valid node of the linked list.
- Do not return anything from your function.
package leetcode.easy; /**
* Definition for singly-linked list. public class ListNode { int val; ListNode
* next; ListNode(int x) { val = x; } }
*/
public class DeleteNodeInALinkedList {
private static void print(ListNode head) {
if (head == null) {
return;
}
while (head != null) {
System.out.print(head.val);
if (head.next != null) {
System.out.print("->");
}
head = head.next;
}
System.out.println();
} public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
} @org.junit.Test
public void test1() {
ListNode listNode1 = new ListNode(4);
ListNode listNode2 = new ListNode(5);
ListNode listNode3 = new ListNode(1);
ListNode listNode4 = new ListNode(9);
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode4.next = null;
print(listNode1);
deleteNode(listNode2);
print(listNode1);
} @org.junit.Test
public void test2() {
ListNode listNode1 = new ListNode(4);
ListNode listNode2 = new ListNode(5);
ListNode listNode3 = new ListNode(1);
ListNode listNode4 = new ListNode(9);
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode4.next = null;
print(listNode1);
deleteNode(listNode3);
print(listNode1);
}
}
LeetCode_237. Delete Node in a Linked List的更多相关文章
- 【4_237】Delete Node in a Linked List
Delete Node in a Linked List Total Accepted: 48121 Total Submissions: 109297 Difficulty: Easy Write ...
- Leetcode-237 Delete Node in a Linked List
#237. Delete Node in a Linked List Write a function to delete a node (except the tail) in a singl ...
- [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点
2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...
- 【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 ...
- 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] 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 ...
- 237. Delete Node in a Linked List【easy】
237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...
- [LeetCode] 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 ...
随机推荐
- Dubbo源码分析:Dubbo协议解码
Dubbo协议解码时序图
- pandas数据类型判断(三)数据判断
1.函数:空值判断 1)判断数值是否为空用 pd.isna,pd.isnull,np.isnan2)判断字符串是否为空用 pd.isna,pd.isnull:3)判断时间是否为空用 pd.isna,p ...
- MCMC蒙特卡罗马尔科夫模型
https://www.cnblogs.com/pinard/p/6645766.html https://blog.csdn.net/saltriver/article/details/521949 ...
- Spring asm
Spring 获取类的详细信息,以及方法时候,是通过asm 字节码进行获取的,在平时中我们也可以依赖spring 进行处理我们的类 //可以获取类的详细信息,比如父类上谁,类上面的注解 ,是否上接口 ...
- .net web api 返回的是xml
var result = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(dto2) ...
- 1.6synchronized代码块
1.synchronized可以使用任意的Object进行加锁,用法比较灵活 ============================================================= ...
- 11、spark内核架构剖析与宽窄依赖
一.内核剖析 1.内核模块 1.Application 2.spark-submit 3.Driver 4.SparkContext 5.Master 6.Worker 7.Executor 8.Jo ...
- Kafka 幂等生产者和事务生产者特性(讨论基于 kafka-python | confluent-kafka 客户端)
Kafka 提供了一个消息交付可靠性保障以及精确处理一次语义的实现.通常来说消息队列都提供多种消息语义保证 最多一次 (at most once): 消息可能会丢失,但绝不会被重复发送. 至少一次 ( ...
- php 压缩文件
<?php $zip = new ZipArchive; $myfile = fopen("test.zip", "w"); chmod(); if ($ ...
- 2、Apache(httpd)之一 三种工作模式
httpd的特性: 高度模块化:core + modules 模块化设计DSO:Dynamic Shared Object MPM:Multipath Processing Modules 多路处理模 ...