Created
March 31, 2017 11:29
-
-
Save EinLama/58f5ea2f3b1805ceed6b6259ce258386 to your computer and use it in GitHub Desktop.
Test bei Make.tv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node | |
attr_accessor :value, :next | |
def initialize(value, _next) | |
@value = value | |
@next = _next | |
end | |
def to_s | |
"#{self.value} -> #{self.next ? self.next.to_s : nil}" | |
end | |
end | |
def reverse(n) | |
prev, _next = nil, nil | |
loop do | |
_next = n.next | |
n.next = prev | |
prev = n | |
if _next | |
n = _next | |
else | |
break | |
end | |
end | |
return n | |
end | |
_4 = Node.new(8, nil) | |
_3 = Node.new(4, _4) | |
_2 = Node.new(7, _3) | |
list = Node.new(1, _2) | |
# n _next | |
# 1 -> 7 -> 4 -> 8 | |
# | |
# 1 <- 7 <- 4 <- 8 | |
puts list | |
x = reverse(list) | |
puts x | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment