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 8702413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:38:51+00:00 2026-06-13T02:38:51+00:00

Has to be O(n) and in-place (space complexity of 1). The code below does

  • 0

Has to be O(n) and in-place (space complexity of 1). The code below does work, but is there a simpler or better way?

public void invert() {
    if (this.getHead() == null)
        return;
    if (this.getHead().getNext() == null)
        return;
    //this method should reverse the order of this linked list in O(n) time
    Node<E> prevNode = this.getHead().getNext();
    Node<E> nextNode = this.getHead().getNext().getNext();
    prevNode.setNext(this.getHead());
    this.getHead().setNext(nextNode);
    nextNode = nextNode.getNext();

    while (this.getHead().getNext() != null)
    {
        this.getHead().getNext().setNext(prevNode);
        prevNode = this.getHead().getNext();
        this.getHead().setNext(nextNode);
        if (nextNode != null)
            nextNode = nextNode.getNext();
    }
    this.head = prevNode;
}
  • 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-13T02:38:53+00:00Added an answer on June 13, 2026 at 2:38 am

    Edited to remove the extra comparison per iteration:

        public void invert() {
            Node<E> prev = null, next = null;;
            if (head == null) return;
            while (true) {
                next = head.getNext();
                head.setNext(prev);
                prev = head;
                if (next == null) return;
                head = next;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has been driving me crazy. I think I have everything in place but
I have a website developed in ASP.NET and VB.NET which has a place for
If my question has already taken place in other post, please excuse me. I
Every place I've worked in the past 15 years has had, on almost every
It has been asked when MeteorJS will have auth in place... I think everyone
Curious if anybody has considered using EnumMap in place of Java beans, particularly value
Im working on a project that has an implementation of JOSSO in place. We
The UserProfile has a one-to-one to 'User' and a many-to-one to Place class UserProfile(
I like that Subversion 1.7 now has all the pristines in one place for
We have a process in place that uploads files to our website. It has

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.