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. commons-dbutils工具栏的编写

    db.properties driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test_db?useUnicode=t ...

  2. 任晓蕊 2019-2020-1 20199302《Linux内核原理与分析》第四周作业

    实验内容 在实验楼的环境中敲入命令 cd LinuxKernel/ qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img ...

  3. C# mysql 处理 事务 回滚 提交

    MySqlConnection myCon; void iniMysql() { //连接数据库 myCon = new MySqlConnection("server=127.0.0.1; ...

  4. javaweb上传文件夹

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...

  5. learning java AWT 剪贴板 传递文本

    import javax.swing.*; import java.awt.*; import java.awt.datatransfer.Clipboard; import java.awt.dat ...

  6. 洛谷 P1855 榨取kkksc03 题解

    P1855 榨取kkksc03 题目描述 洛谷2的团队功能是其他任何oj和工具难以达到的.借助洛谷强大的服务器资源,任何学校都可以在洛谷上零成本的搭建oj并高效率的完成训练计划. 为什么说是搭建oj呢 ...

  7. Solution

    小五的游戏 小碎骨的子集 芙兰朵露的框框 ⑨要求和

  8. 数据结构实验之排序七:选课名单 (SDUT 3404)

    #include <stdio.h> #include <string.h> #include <stdlib.h> struct node { char data ...

  9. fuck-KUNLUN昆仑ECRS会员管理系统

    写代码要认证一点,多一点测试, navigator.appName == 'Netscape'这么垃圾的代码都能写出来,握草. 怕时间就了就忘记怎么搞的了,MD经过前端各种断点,找到了这个垃圾玩意儿. ...

  10. Pytest权威教程05-Pytest fixtures:清晰 模块化 易扩展

    目录 Pytest fixtures:清晰 模块化 易扩展 Fixtures作为函数参数使用 Fixtures: 依赖注入的主要例子 conftest.py: 共享fixture函数 共享测试数据 生 ...