Logo

dev-resources.site

for different kinds of informations.

Reversing a Linked List: A Hallway Analogy

Published at
4/28/2023
Categories
linkedlists
pointers
reverse
modules
Author
byteshiva
Author
9 person written this
byteshiva
open
Reversing a Linked List: A Hallway Analogy

Imagine that you are walking through a hallway that has several doors on either side. Each door has a name written on it, and each name is related to the one next to it in some way. You are holding a piece of paper that has the names of all the doors in the hallway, but they are in the wrong order.

As you walk down the hallway, you start to feel frustrated because you are not sure which door to go through next. Suddenly, you remember that you can use the paper to help you figure out the correct order of the doors.

You start at the end of the hallway, and look at the last name on the paper. You walk through the door with that name on it, and then look at the name on the previous line of the paper. You walk through the door with that name on it, and repeat the process until you reach the beginning of the hallway.

As you walk through each door, you cross it off on the paper to keep track of where you have been. When you reach the end of the hallway, you realize that you have successfully reversed the order of the doors!

In programming terms, the paper with the names is like a linked list, where each name is a node in the list that points to the next node. Reversing the linked list involves changing the pointers so that each node points to the previous node, instead of the next node. The hallway with the doors is just a visual representation of the linked list, where each door corresponds to a node in the list.

linkedlist.jl

module LinkedList

  export Person, reverse_list

  mutable struct Person
      name::String
      next::Union{Person, Nothing}
  end

  function reverse_hallway(person::Person)
      current_orig = person
      previous = nothing
      while current_orig != nothing
          next_person = current_orig.next
          current_orig.next = previous
          previous = current_orig
          current_orig = next_person
      end
      return previous
  end

end # end of module
Enter fullscreen mode Exit fullscreen mode

main.jl

include("linkedlist.jl")

using .LinkedList: Person, reverse_hallway

# example linked list of people
person4 = Person("Alice", nothing)
person3 = Person("Bob", person4)
person2 = Person("Charlie", person3)
person1 = Person("Dave", person2)

function printoriginal_order(person1) 
  current = person1
  while current != nothing
    println(current.name)
    current = current.next
  end
end

function print_reverse_order(person1) 
  person1 = reverse_hallway(person1)
    # print new order
  current = person1
  while current != nothing
      println(current.name)
      current = current.next
  end
end

printoriginal_order(person1)
println("---")
print_reverse_order(person1)
Enter fullscreen mode Exit fullscreen mode

Demo — Code Walkthrough

https://medium.com/media/824c9cb1a26e885f224810682dd19253/href

pointers Article's
30 articles in total
Favicon
CS50 - Week 4
Favicon
Pointers in Modern C
Favicon
Go: Pointers & Memory Management
Favicon
Pointers and Arrays
Favicon
Understanding Memory Management, Pointers, and Function Pointers in C
Favicon
Pointers : what are they pointing to?
Favicon
Pointers in Go Programming Language
Favicon
An ode to Stacks and Pointers in Go!
Favicon
The Absolute Minimum Every Software Developer Must Know About Pointers
Favicon
Pointers in C programming.
Favicon
How constant is const in C?
Favicon
Pointers in C Programming: How Hard?🤔
Favicon
In C++, is a free function taking a struct as an argument faster than a class with a member function to do the same thing?
Favicon
How to assign an Address, contained inside a string, to a pointer in C
Favicon
Understanding pointers in Go
Favicon
Reversing a Linked List: A Hallway Analogy
Favicon
Exploring the Power of Pointers in C Programming
Favicon
Learn It Once: “Golang Pointers are Powerful”
Favicon
C++23: std::out_ptr and std::inout_ptr
Favicon
Pointers , Arrays & Strings in C
Favicon
C - Even more pointers, arrays and strings
Favicon
C - Pointers, arrays and strings
Favicon
A common pitfall when using sizeof() with pointers
Favicon
Pointers
Favicon
Storing references of pointers in containers in C++
Favicon
C++ basics: Pointers vs iterators
Favicon
I broke production 3 times in 3 weeks - Part II
Favicon
WTF*&n is Pointers in Golang (Bahasa Version)
Favicon
I broke production 3 times in 3 weeks - Part I
Favicon
Golang 101: İşaretçiler (Pointers)

Featured ones: