237. Delete Node in a Linked List

Easy

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的更多相关文章

  1. 【4_237】Delete Node in a Linked List

    Delete Node in a Linked List Total Accepted: 48121 Total Submissions: 109297 Difficulty: Easy Write ...

  2. 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 ...

  3. [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 ...

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

  5. 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 ...

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

  9. [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 ...

随机推荐

  1. js 全选反选

    <th><input type="checkbox" id="checkall" name="checkall" oncl ...

  2. 常用的两种web单点登录SSO的实现原理

    单点登录SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户的一次登录能得到其他所有系统的信任.单点登录在大型网站里使用得 ...

  3. How To Set The Hostname On Ubuntu Or Debian?

    $ sudo hostnamectl set-hostname your-hostname $ sudo vim /etc/hosts Open the hosts file and add the ...

  4. Session的应用——三天免登录

    1.使用Cookie实现的登录的不足: protected void doGet(HttpServletRequest request, HttpServletResponse response) t ...

  5. RookeyFrame 信息 常用信息整理

    博客 https://www.cnblogs.com/rookey/ gitee的地址: https://gitee.com/rookey/Rookey.Frame-v2.0 https://gite ...

  6. python-图像目标监测(1)识别答题卡

    # -*- coding: utf-8 -*- """ Created on Thu Dec 20 16:05:10 2018 @author: leizhen.liu ...

  7. mysql创建账号、授权、数据导出、导入

    1.账号创建及授权 grant all privileges on *.* to 'yangxin'@'%' identified by 'yangxin123456' with grant opti ...

  8. php读取邮件

    <?php header("Content-type: text/html; charset=utf-8"); class mail { private $server='' ...

  9. P2016 战略游戏——树形DP大水题

    P2016 战略游戏 树形DP 入门题吧(现在怎么是蓝色标签搞不懂): 注意是看见每一条边而不是每一个点(因为这里错了好几次): #include<cstdio> #include< ...

  10. 设置虚拟机ip地址

    当我们在linux中输入命令ifconfig来查找ip地址的时候,可能会出现只有ipv6,而没有inet的情况,这时需要自己设定,有两种方式可供选择: 1,命令 ifconfig 设备名(如eth0) ...