zoukankan      html  css  js  c++  java
  • Leet Code OJ 237. Delete Node in a Linked List [Difficulty: Easy]

    题目:
    Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

    Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

    翻译:
    写一个方法去删除单链表的一个节点(非尾节点),仅仅给出訪问这个节点的參数。

    假定链表是1 -> 2 -> 3 -> 4,给你的节点是第三个节点(值为3),这个链表在调用你的方法后应该变为 1 -> 2 -> 4。

    分析:
    删除链表的节点的常规做法是改动上一个节点的next指针。然而这边并没有提供上一个节点的訪问方式。故转变思路改为删除下一节点。直接将下一个节点的val和next赋值给当前节点。

    代码:

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public void deleteNode(ListNode node) {
            node.val=node.next.val;
            node.next=node.next.next;
        }
    }
  • 相关阅读:
    集合
    字典
    Visual Studio 2017 发布
    表现设身处地的方法:杜彬(Ben Duffy)方法
    Can RemObjects SDK parameters be passed via the URI?
    转:RemObjects SDK 简介
    转:RemObject 服务器端自调用的方法
    转: Delphi多层开发方案比较
    Embed
    log4d 的使用(转)
  • 原文地址:https://www.cnblogs.com/lytwajue/p/7226061.html
Copyright © 2011-2022 走看看