zoukankan      html  css  js  c++  java
  • 在O(1)时间复杂度删除链表节点

    给定一个单链表中的一个等待被删除的节点(非表头或表尾)。请在在O(1)时间复杂度删除该链表节点。

    例如: Linked list is 1->2->3->4,    and given node 3,        delete the node in place 1->2->4

    思路:

    1、将该节点的值赋为下一个节点的值

    2、该节点的next指向下下一个节点

    /**
     * Definition for ListNode.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int val) {
     *         this.val = val;
     *         this.next = null;
     *     }
     * }
     */


    public class Solution {
        /*
         * @param node: the node in the list should be deletedt
         * @return: nothing
         */
        public void deleteNode(ListNode node) {
            // write your code here
            node.val = node.next.val;
            node.next = node.next.next;
        }
    }

  • 相关阅读:
    spi详解
    spi协议
    C语言break,return
    通信协议
    传输层
    网络层
    数据链路层
    物理层
    无线通信
    cpu设计过程
  • 原文地址:https://www.cnblogs.com/yanernanfei/p/7671275.html
Copyright © 2011-2022 走看看