Delete Node in the Middle of Singly Linked List
题目:
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.
分析:
这个题目有意思的,如果是在c++可以直接access这个地址栏,就可以暴力让
*node = *(node->next);
而在java不知道如何暴力access这个地址,于是曲线救国,把node变成跟node.next一样,再删掉node.next
以前是不知道这第二个方法的...
解法:
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 next = node.next;
node.val = next.val;
node.next = next.next;
}
}