237 - Delete Node in the Middle of Singly Linked List
Written on October 21, 2015
Tweet
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.
public class Solution {
/**
* @param node: the node in the list should be deleted
* @return: nothing
*/
public void deleteNode(ListNode node) {
// write your code here
if(node == null) return;
if(node.next != null){
node.val = node.next.val;
node.next = node.next.next;
}
}
}
class Solution(object):
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
if not node:
return
if node.next:
node.val = node.next.val
node.next = node.next.next