Class: DSA::LinkedList::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/dsa-ruby/linked_list.rb

Overview

Node for a singly linked list.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val = 0, nxt = nil) ⇒ Node

Initialize a new singly linked list node.

Parameters:

  • val (Object) (defaults to: 0)

    the value to store

  • nxt (Node, nil) (defaults to: nil)

    the next node (default: nil)



19
20
21
22
# File 'lib/dsa-ruby/linked_list.rb', line 19

def initialize(val = 0, nxt = nil)
  @val = val
  @next = nxt
end

Instance Attribute Details

#nextNode?

Returns the next node in the list.

Returns:

  • (Node, nil)

    the next node in the list



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dsa-ruby/linked_list.rb', line 12

class Node
  attr_accessor :val, :next

  # Initialize a new singly linked list node.
  #
  # @param val [Object] the value to store
  # @param nxt [Node, nil] the next node (default: nil)
  def initialize(val = 0, nxt = nil)
    @val = val
    @next = nxt
  end
end

#valObject

Returns the value stored in the node.

Returns:

  • (Object)

    the value stored in the node



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dsa-ruby/linked_list.rb', line 12

class Node
  attr_accessor :val, :next

  # Initialize a new singly linked list node.
  #
  # @param val [Object] the value to store
  # @param nxt [Node, nil] the next node (default: nil)
  def initialize(val = 0, nxt = nil)
    @val = val
    @next = nxt
  end
end