-1

I am implementing a reversed linked list using a stack. Class Stack has methods push and pop, which turn a linked list into a stack from which the last element can be extracted in order to reverse the order.

I've encountered a similar situation here, and I'm trying to add a method to that base case. I'm trying to implement a reverse_list method that makes use of the Stack class's push and pop in order to change the pointer and reverse the linked list.

This is what I tried:

class LinkedListNode
  attr_accessor :value, :next_node

  def initialize(value, next_node=nil)
    @value = value
    @next_node = next_node
  end
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

class Stack
    attr_reader :data

    def initialize
        @data = nil 
    end

    def push(value)
      @data = LinkedListNode.new(value, @data) 
    end

    def pop
      return nil if @data.nil?
      returning_value = @data.value
      @data = @data.next_element
      returning_value
    end
end

def reverse_list(list)
    stack = Stack.new.push(list.value)
    list = list.next_node

    while list
      stack.push(list.value)
      list = list.next_node
    end

    stack.pop
end

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

revlist = reverse_list(node3)
print_values(revlist)
# should return 37 --> 99 --> 12 --> nil

I get errors when calling the Stack class in the reverse_list method (undefined method push for <Context::LinkedListNode:0x00000001c5e0a8>).

I'm at a loss as to why I cannot use Stack, push, and pop inside reverse_list. Any tips as to how I could go about implementing reverse_list will be well received.

4 Answers 4

2

The following pushes the elements into the stack, and then reverses them:

def reverse_list(list) 
    stack = Stack.new

    while list
      stack.push(list.value)
      list = list.next_node
    end

  LinkedListNode.new(stack.pop, stack.data)
end
Sign up to request clarification or add additional context in comments.

Comments

1

stack = Stack.new.push(list.value)

In your example, variable stack is an instance of LinkedListNode as push method is returning instance of linked list. So no method error is throwing. You have to create instance of Stack first like stack = Stack.new which woulb be an empty stack and then do push and pop operation using this instance.

Comments

0

Your pop Method within your Stack class is calling for "next_element", but I think you should be referring to the "next_node" variable from the LinkedListNode class. I would re-write your "pop" method this way:

def pop
 if @data.nil?
   nil
 else
   returning_value = @data.value
   @data = @data.next_node
   returning_value
 end
end

I would also rewrite the method to reverse the linked list like this:

def reversed_linked_list(list)
   stack = Stack.new
  while list
   stack.push(list.value)
   list = list.next_node
  end
  LinkedListNode.new(stack.pop, stack.data)
end

Comments

-1

Iterate through the list adding its contents to a stack until the end of the list. After that, the following algorithm can be applied.

root = stack.pop() # last element
current = root
while not stack.empty():       # move in reverse direction
    current.next = stack.pop() # next element in stack is the next node
    current = current.next     # continue to the next node
current.next = nil             # mark end of the list

2 Comments

I think you're writing Python here. It's a Ruby question.
Please don't do that. If the question asks for Ruby, do Ruby. If you don't know Ruby, that's fine, but this isn't helping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.