首页 > 其他 > 详细

lintcode-easy-Delete Node in the Middle of Singly Linked List

时间:2016-02-24 18:56:21      阅读:291      评论:0      收藏:0      [点我收藏+]

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

Example

Given 1->2->3->4, and node 3. return 1->2->4

 

删除的方法就是用后面的值覆盖前面的值,注意避免OBOB

/**
 * 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 deleted
     * @return: nothing
     */
    public void deleteNode(ListNode node) {
        // write your code here
        ListNode p = node;
        
        while(p.next.next != null){
            p.val = p.next.val;
            p = p.next;
        }
        p.val = p.next.val;
        p.next = null;
        
        return;
    }
}

 

lintcode-easy-Delete Node in the Middle of Singly Linked List

原文:http://www.cnblogs.com/goblinengineer/p/5213808.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!