[2019.11.29] algorithm learning record - delete the list of nodes

Algorithm - delete the list of nodes


Please write a function that makes it possible to delete a list given (non-end) node, you will only be required for a given node is removed.

A list of existing - head = [4,5,1,9], it can be expressed as:

List example

Example 1:

Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
After a given second node in the list you value of 5, then call your function: Dictionary the list strain 4 -> 1 -> 9.
example 2:

Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
After a given value 1 of the list you third node, then call your function: Dictionary the list strain 4 -> 5 -> 9.

Description:

  • List comprising at least two nodes.
  • The value of all the nodes in the list are unique.
  • Non given node and the end node must be a valid node in the linked list.
  • Do not return any results from your functions.

Source: stay button (LeetCode)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public void deleteNode(ListNode node) {
            node.val = node.next.val;
            node.next = node.next.next;
    }
}

Ideas
at the point you want to delete a node to node

Published 17 original articles · won praise 0 · Views 336

Guess you like

Origin blog.csdn.net/cletitia/article/details/103318888