Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7895927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:45:43+00:00 2026-06-03T07:45:43+00:00

I created a singly LinkedList class with ruby. Everything went well till trying to

  • 0

I created a singly LinkedList class with ruby.
Everything went well till trying to reverse the linked list.

It does not reverse linkedlist by this method, but when I add
@head.next = nil
after left_tmp = @head in reverse method, it just works fine.

I couldn’t figure out why it works when I add that, does anyone have the explanation?

BTW I am fairly new to ruby, so please don’t hesitate to tell me if there are some other things that are not “Good Practice in Ruby“.

Here is classes and relevant methods:

class LlNode
  attr_reader :data
  attr_accessor :next

  def initialize(val=nil)
    @data = val
    @next = nil
  end

  def to_s
    "node_data=#{@data}"
  end
end

class LinkedList
  def initialize
    @list = []
    @head = LlNode.new
  end

  def insert(val)
    n = LlNode.new val
    # List is empty
    if is_empty?
      @head = n
    else
      n.next = @head
      @head = n
    end
    self
  end

  def reverse
    return if is_empty? or @head.next.nil?

    curr = @head.next
    right_tmp = curr.next
    left_tmp = @head

    while curr != nil
      curr.next = left_tmp
      left_tmp = curr
      curr = right_tmp
      right_tmp = right_tmp.next unless right_tmp.nil?
    end
    @head = left_tmp
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:45:44+00:00Added an answer on June 3, 2026 at 7:45 am

    When you’re reversing a linked list, the first node becomes the last. In a singly-linked list, the last node’s next pointer points to null. @head, which is initially your first node becomes the last. That’s why you add the @head.next = nil.

    Edit: Simulating a dry-run to better explain the problem
    Assume two nodes in the linked list: 1->2

    curr = @head.next  (2)
    right_tmp = curr.next  (nil)
    left_tmp = @head  (1)
    

    First iteration of the while loop:

    curr.next = left_tmp   ( 1 <-> 2)
    left_tmp = curr     (2)
    curr = right_tmp    (nil)
    right_tmp = right_tmp.next unless right_tmp.nil?    (nil)
    

    There is no second iteration since curr == nil

    Now:

    @head = left_tmp  (@head points to '2')
    

    Final linked list state is:

    1 <-> 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to sort a singly linked list which has been created and
I am trying to implement a simple singly linked list of integers which are
I have created a single linked list. Everything works fine. I just want to
I'm trying to create a singly linked list from an input text file for
i'm trying to create a simple single linked list in python. (i know there
I need to break a singly linked list into smaller linked lists after every
I created a Dialog with single-choice list items: final CharSequence[] items = {Red, Green,
Currently I have created a ABCFactory class that has a single method creating ABC
I was practicing single linked list in c++ (practicing how to find the beginning
i have created a single page application of showing phonebook data in a list.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.