资讯专栏INFORMATION COLUMN

[LintCode] Delete Node in the Middle of Singly Li

lowett / 658人阅读

Problem

Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.

Note

就是把node.next.val赋给node,然后删掉node.next,用node直接连接node.next.next

Solution
public class Solution {
    public void deleteNode(ListNode node) {
        if (node == null) return;
        if (node.next != null) {
            node.val = node.next.val;
            node.next = node.next.next;
        }
        return;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65536.html

相关文章

  • [Lintcode] Nth to Last Node in List 链表倒数第N个节点

    摘要:递归法复杂度时间空间思路当递归到链表尾部时返回,每次返回时长度加,一旦长度为时记录下该节点。双指针法复杂度时间空间思路用两个指针,快指针先走步,然后快慢指针同时开始走,保持的距离,这样当快指针到达末尾时,慢指针就是倒数第个。 Nth to Last Node in List Find the nth to last element of a singly linked list. ...

    CoXie 评论0 收藏0
  • [LintCode/LeetCode] Nth to Last Node in List

    摘要:依然是一道找倒数第个结点的链表题,用双指针做。先走,然后和一起走,直到为,的位置就是倒数第个位置。 Problem Find the nth to last element of a singly linked list. The minimum number of nodes in list is n. Example Given a List 3->2->1->5->null ...

    Salamander 评论0 收藏0
  • [LintCode/LeetCode] Add Two Numbers

    Problem You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1s digit is at the head of the list. Write a f...

    hedzr 评论0 收藏0
  • [LintCode/LeetCode] Intersection of Two Linked Lis

    Problem Write a program to find the node at which the intersection of two singly linked lists begins. Example The following two linked lists: A: a1 → a2 ↘ ...

    OldPanda 评论0 收藏0
  • [LeetCode/LintCode] Odd Even Linked List

    Problem Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. Example Example:Given...

    awokezhou 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<