Fork me on GitHub

Lintcode 372. O(1)时间复杂度删除链表节点

 

-----------------------------------

 

AC代码:

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

 

 

题目来源: http://www.lintcode.com/zh-cn/problem/delete-node-in-the-middle-of-singly-linked-list/

posted @ 2017-01-01 23:43  CC11001100  阅读(281)  评论(0编辑  收藏  举报