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

  • Home
  • SEARCH
  • 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 6469745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:01:46+00:00 2026-05-25T06:01:46+00:00

In Scala, there is reverse method for lists. What is the complexity of this

  • 0

In Scala, there is reverse method for lists. What is the complexity of this method? Is it better to simply use the original list and always remember that the list is the reverse of what we expect, or to explicitly use reverse before operating on it.

EDIT: What I am really interested in is to get the last two elements of the original list (or the first two of the reversed list).

So I would do something like:

val myList = origList.reverse
val a = myList(0)
val b = myList(1)

This is not in a loop, just a one-time thing in my library… but if someone else uses the library and puts it in a loop, it is not under my control.

  • 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-05-25T06:01:46+00:00Added an answer on May 25, 2026 at 6:01 am

    Looking at the source, it’s O(n) as you might reasonably expect:

    override def reverse: List[A] = {
      var result: List[A] = Nil
      var these = this
      while (!these.isEmpty) {
        result = these.head :: result
        these = these.tail
      }
      result
    }
    

    If in your code you’re able to iterate through the list in reverse order at the same cost of iterating in forward order, then it would be more efficient to do this rather than reversing the List.

    In fact, if your alternative operation which involves using the original list works in less than O(n) time, then there’s a real argument for going with that. Making an algorithm asymptotically faster will make a huge difference if you ever rely on it more (especially if used inside other loops, as oxbow_lakes points out below).

    On the whole though I’d expect that anything where you’re reversing a list means that you care about the relative ordering of a non-trivial number of elements, and so whatever you’re doing is inherently O(n) anyway. (This might not be true for other data structures such as a binary tree; but lists are linear, and in the extreme case even reverse . head can’t be done in O(1) time with a singly-linked list.)

    So if you’re choosing between two O(n) options – for the vast majority of applications, shaving a few nanoseconds off the iteration time isn’t going to really gain you anything. Hence it would be “best” to make your code as readable as possible – which means calling reverse and then iterating, if that’s closest to your intention.

    (And if your app is too slow, and profiling shows that this list manipulation is a hotspot, then you can think about how to make it more efficient. Which by that point may well involve a different option to both of your current candidates, given the extra context you’ll have at that point.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Scala and there is a thing that I can't find out about
As the title says, is there any Scala library that exports functions to convert,
I wanted to ask if there is a list of annotations for Scala 2.8.0?
There is a demo by IBM that shows how easy Reverse AJAX can be
I recently stumbled over this post , which introduces the collect method for Scala
Is there any difference between ::: and ++ for concatenating lists in Scala? scala>
Can I use scala List in Java, like : import scala.collection.immutable.List; class HelloScalaList {
in Scala there is syntactic sugar by calling a method when the method call
In Scala there are two ways to define a method which takes no argument
Is there a Scala SWT wrapper/API that has the most features and is most

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.