Insert node at end of linked list in ruby

Write a program which is create and add linked list node at the end (tail, last) position in ruby.

Suppose we are inserted the following (10, 20, 30 ,40 ,50) node in a sequence.

insert node at end of linked list
#  Ruby Program for
#  Insert linked list element at end position

#  Linked list node
class LinkNode 
    # Define the accessor and reader of class LinkNode
    attr_reader :data, :next
    attr_accessor :data, :next
    def initialize(data) 
        self.data = data
        self.next = nil
    end

end

class SingleLL 
    # Define the accessor and reader of class SingleLL
    attr_reader :head
    attr_accessor :head
    def initialize() 
        self.head = nil
    end

    #  Add new node at the end of linked list
    def addNode(value) 
        # Create  node
        node = LinkNode.new(value)
        if (self.head == nil) 
            self.head = node
        else
 
            temp = self.head
            #  find last node
            while (temp.next != nil) 
                #  Visit to next node
                temp = temp.next
            end

            #  Add node at last position
            temp.next = node
        end

    end

    # Display all Linked List elements
    def display() 
        if (self.head != nil) 
            temp = self.head
            while (temp != nil) 
                #  Display node value
                print("  ", temp.data)
                #  Visit to next node
                temp = temp.next
            end

        else
 
            print("Empty Linked list\n")
        end

    end

end

def main() 
    sll = SingleLL.new()
    #  Linked list
    #  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
    sll.addNode(1)
    sll.addNode(2)
    sll.addNode(3)
    sll.addNode(4)
    sll.addNode(5)
    sll.addNode(6)
    sll.addNode(7)
    sll.addNode(8)
    print(" Linked List \n")
    sll.display()
end

main()
 Linked List
  1  2  3  4  5  6  7  8

Time complexity of above program is O(n). We can optimize above algorithm using one extra pointer. Which is hold the reference of last node. Below are implementation of this logic.

#  Ruby Program for
#  Insert linked list element at end position set B

#  Linked list node
class LinkNode 
    # Define the accessor and reader of class LinkNode
    attr_reader :data, :next
    attr_accessor :data, :next
    def initialize(data) 
        self.data = data
        self.next = nil
    end

end

class SingleLL 
    # Define the accessor and reader of class SingleLL
    attr_reader :head, :tail
    attr_accessor :head, :tail
    def initialize() 
        self.head = nil
        self.tail = nil
    end

    #  Add new node at the end of linked list
    def addNode(value) 
        #  Create a new node
        node = LinkNode.new(value)
        if (self.head == nil) 
            self.head = node
        else
 
            self.tail.next = node
        end

        self.tail = node
    end

    #  Display linked list element
    def display() 
        if (self.head == nil) 
            return
        end

        temp = self.head
        #  iterating linked list elements
        while (temp != nil) 
            print(temp.data ," → ")
            #  Visit to next node
            temp = temp.next
        end

        print(" NULL\n")
    end

end

def main() 
    sll = SingleLL.new()
    #  Linked list
    #  10 → 20 → 30 → 40 → 50 → NULL
    sll.addNode(10)
    sll.addNode(20)
    sll.addNode(30)
    sll.addNode(40)
    sll.addNode(50)
    print("Linked List \n")
    sll.display()
end

main()
Linked List 
10 → 20 → 30 → 40 → 50 →  NULL

Time complexity of above program is O(1).



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved