|
| 1 | +/* |
| 2 | + * @lc app=leetcode id=237 lang=java |
| 3 | + * |
| 4 | + * [237] Delete Node in a Linked List |
| 5 | + * |
| 6 | + * https://leetcode.com/problems/delete-node-in-a-linked-list/description/ |
| 7 | + * |
| 8 | + * algorithms |
| 9 | + * Easy (52.04%) |
| 10 | + * Total Accepted: 277.2K |
| 11 | + * Total Submissions: 523.8K |
| 12 | + * Testcase Example: '[4,5,1,9]\n5' |
| 13 | + * |
| 14 | + * Write a function to delete a node (except the tail) in a singly linked list, |
| 15 | + * given only access to that node. |
| 16 | + * |
| 17 | + * Given linked list -- head = [4,5,1,9], which looks like following: |
| 18 | + * |
| 19 | + * |
| 20 | + * |
| 21 | + * |
| 22 | + * |
| 23 | + * Example 1: |
| 24 | + * |
| 25 | + * |
| 26 | + * Input: head = [4,5,1,9], node = 5 |
| 27 | + * Output: [4,1,9] |
| 28 | + * Explanation: You are given the second node with value 5, the linked list |
| 29 | + * should become 4 -> 1 -> 9 after calling your function. |
| 30 | + * |
| 31 | + * |
| 32 | + * Example 2: |
| 33 | + * |
| 34 | + * |
| 35 | + * Input: head = [4,5,1,9], node = 1 |
| 36 | + * Output: [4,5,9] |
| 37 | + * Explanation: You are given the third node with value 1, the linked list |
| 38 | + * should become 4 -> 5 -> 9 after calling your function. |
| 39 | + * |
| 40 | + * |
| 41 | + * |
| 42 | + * |
| 43 | + * Note: |
| 44 | + * |
| 45 | + * |
| 46 | + * The linked list will have at least two elements. |
| 47 | + * All of the nodes' values will be unique. |
| 48 | + * The given node will not be the tail and it will always be a valid node of |
| 49 | + * the linked list. |
| 50 | + * Do not return anything from your function. |
| 51 | + * |
| 52 | + * |
| 53 | + */ |
| 54 | +/** |
| 55 | + * Definition for singly-linked list. |
| 56 | + * public class ListNode { |
| 57 | + * int val; |
| 58 | + * ListNode next; |
| 59 | + * ListNode(int x) { val = x; } |
| 60 | + * } |
| 61 | + */ |
| 62 | +class Solution { |
| 63 | + public void deleteNode(ListNode node) { |
| 64 | + node.val = node.next.val; |
| 65 | + node.next = node.next.next; |
| 66 | + } |
| 67 | +} |
| 68 | + |
0 commit comments