Created
July 29, 2019 03:33
-
-
Save jasonknight/4bead2baa272a4ade0ab9ac56584fff1 to your computer and use it in GitHub Desktop.
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 :val,:nxt | |
def initialize(val,nxt) | |
@val = val | |
@nxt = nxt | |
end | |
end | |
def print_nodes(head) | |
while head do | |
puts head.val | |
head = head.nxt | |
end | |
end | |
def reverse(head,prev) | |
if not head.nxt then | |
head.nxt = prev | |
return head | |
end | |
cnxt = head.nxt | |
head.nxt = prev | |
return reverse(cnxt,head) | |
end | |
first = Node.new('1',Node.new('2',Node.new('3',Node.new('4',nil)))) | |
first = reverse(first,nil) | |
print_nodes(first) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or, the obligatory code golf: