372. Delete Node in the Middle of Singly Linked List

Leave a comment

June 1, 2016 by oneOokay

It is marked as “EASY” but…

KEY: It doesn’t give you the head node. So you have to “link” previous node to the next node somehow.

SOLUTION: copy node.next to node and handle the links and values appropriately.

 

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 || node.next == null)
return;

ListNode next = node.next;
node.val = next.val;
node.next = node.next.next;
}
}

 

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: