-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdelete_middle_node.rb
38 lines (33 loc) · 1003 Bytes
/
delete_middle_node.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# CtCI 6th Edition Problem 2.3
# Delete Middle Node: Implement an algorithm to delete a node in the middle (i.e., any node but
# the first and last node, not necessarily the exact middle) of a singly linked list, given only access to
# that node.
# EXAMPLE
# Input: the node c from the linked list a->b->c->d->e->f
# Result: nothing is returned, but the new linked list looks like a->b->d->e->f
class Node
attr_accessor :data, :next
def initialize(data)
@data = data
@next = nil
end
def to_s
data_str = ''
current = self
until current.nil?
data_str << "->#{current.data}"
current = current.next
end
data_str[2..data_str.size]
end
end
# O(1) operation
def delete_middle_node(node)
# This works by putting the next node's data into this node,
# setting next to next.next, and then removing the next node.
temp = node.next
node.data = node.next.data
node.next = node.next.next
temp.next = nil # Remove pointer for GC
return nil
end