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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:21:11+00:00 2026-05-24T03:21:11+00:00

I realize this sort of data structure is better done with built in list

  • 0

I realize this sort of data structure is better done with built in list type, but I’m trying to understand this more for academic reasons. Given that I have a linked list like this:

a -> b -> c -> d -> e -> f

I would like to change the references to

b -> a -> d -> c -> f -> e

In other words every pair gets switched. I am using these two classes to create a linked list.

class Node:
    def __init__(self):
        self.cargo = None 
        self.next = None 

class LinkedList:
    def __init__(self):
        self.cur_node = None

    def add_node(self, cargo):
        new_node = Node() 
        new_node.cargo = cargo
        new_node.next = self.cur_node 
        self.cur_node = new_node

    def print_list(self):
        node = self.cur_node
        while node:
            print node.cargo
            node = node.next

    def reorder(self):
        # missing code here!


ll = LinkedList()
ll.add_node("a")
ll.add_node("b")
ll.add_node("c")
ll.add_node("d")
ll.add_node("e")
ll.add_node("f")
ll.reorder()
ll.print_list()

Any ideas?

  • 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-24T03:21:12+00:00Added an answer on May 24, 2026 at 3:21 am

    Sometimes the best thing is to first think “how fast would an optimal solution be?” This seems pretty apparently O(length), so something that runs through the list, preferably once, is going to be about as good as you can do.

    Given that, you’re probably going to find the simplest choice is best. In pseudocode, it would be

     get the first element in left
     get the second element in right
     append them to a new list as right->left
     repeat until you run out of list.
    

    As Matt and Jodaka note, you do need to decide what to do with an odd-length list, if an odd-length list is permitted at all.

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

Sidebar

Related Questions

I realize this is more of a hardware question, but this is also very
I realize this would violate convention, but I'm curious to know if you can
I realize this perhaps a naive question but still I cant figure out how
I realize this is a rather odd request, but I was wondering if anyone
I realize this is probably a hopelessly newbie question, but what is the difference
I realize this is a basic question but I have searched online, been to
I realize this sounds a little crazy, but I'm working on a project for
(Note: I realize this is close to How do you document your database structure?
I didn't realize this, but apparently Python's strftime function doesn't support dates before 1900:
For example I have got this structure: <a href='#' id='by_id'>Sort by id</a> <a href='#'

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.