码迷,mamicode.com
首页 > 编程语言 > 详细

372. Delete Node in a Linked List【LintCode java】

时间:2018-07-14 13:11:45      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:his   imp   没有   code   一个   body   int   node   ==   

Description

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

Example

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

解题:删除指定的结点。由于java没有delete 或者 free,如果要要删除的结点是最后一个节点,在不知道头结点的情况下,是不能删除的。代码如下:

/**
 * 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
        if(node == null || node.next == null)
            return;
        
        node.val = node.next.val;
        node.next = node.next.next;
        return;
        
    }
}

 

372. Delete Node in a Linked List【LintCode java】

标签:his   imp   没有   code   一个   body   int   node   ==   

原文地址:https://www.cnblogs.com/phdeblog/p/9308929.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!