美文网首页
删除链表中的节点

删除链表中的节点

作者: Haward_ | 来源:发表于2019-10-15 21:51 被阅读0次

    题目:
    请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。

    现有一个链表 -- head = [4,5,1,9],它可以表示为:

    示例 1:

    输入: head = [4,5,1,9], node = 5
    输出: [4,1,9]
    解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
    示例 2:

    输入: head = [4,5,1,9], node = 1
    输出: [4,5,9]
    解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/delete-node-in-a-linked-list

    思想:通过复制下一个节点实现

    /**
     * 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;
        }
    }
    

    相关文章

      网友评论

          本文标题:删除链表中的节点

          本文链接:https://www.haomeiwen.com/subject/vyyemctx.html