Skip to content

Instantly share code, notes, and snippets.

@pshushereba
Created March 2, 2016 22:59
Show Gist options
  • Select an option

  • Save pshushereba/077c3ad22aad5a8769c9 to your computer and use it in GitHub Desktop.

Select an option

Save pshushereba/077c3ad22aad5a8769c9 to your computer and use it in GitHub Desktop.

Revisions

  1. pshushereba created this gist Mar 2, 2016.
    63 changes: 63 additions & 0 deletions linkedlist1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    class LinkedListNode
    attr_accessor :value, :next_node

    def initialize(value, next_node=nil)
    @value = value
    @next_node = next_node
    end

    def print_values(list_node)
    if list_node
    print "#{list_node.value} --> "
    print_values(list_node.next_node)
    else
    print "nil\n"
    return
    end
    end

    def reverse_list(list)
    # ADD CODE HERE

    while list
    # ADD CODE HERE
    list = list.next_node
    end

    # ADD CODE HERE
    end

    end

    class Stack
    attr_reader :data

    def initialize
    @data = nil
    end

    # Push a value onto the stack
    def push(value)
    # IMPLEMENT ME!
    end

    # Pop an item off the stack.
    # Remove the last item that was pushed onto the
    # stack and return the value to the user
    def pop
    # I RETURN A VALUE
    end

    end

    node1 = LinkedListNode.new(37)
    node2 = LinkedListNode.new(99, node1)
    node3 = LinkedListNode.new(12, node2)

    print_values(node3)

    puts "-------"

    revlist = reverse_list(node3)

    print_values(revlist)