Solved

Reverse Concat List

Posted on 2011-02-28
9
648 Views
Last Modified: 2013-12-17
Experts,

I have a list, and I need to reverse it and then concat it to itself. This might be really silly question.

Example : Original List  1 2 3 4
                 Altered List  1 2 3 4 4 3 2 1

Reverse() method returns a Void. I was planning on DList.Concat(DList.Reverse());

Whats the fasted way to achieve this?
0
Comment
Question by:San24
  • 4
  • 3
  • 2
9 Comments
 
LVL 8

Expert Comment

by:dericstone
Comment Utility
Just thinking about it without testing:
List<int> list1 = new List<int>();
List<int> list2 = new List<int>();

// fill list1

list1.CopyTo(list2);
list2.Reverse();

list1.AddRange(list2);

Open in new window

0
 

Author Comment

by:San24
Comment Utility
Is there any other way to do it?
0
 
LVL 8

Expert Comment

by:dericstone
Comment Utility
What is your ultimate goal? Are you looking for the fastest way to do this? If so, it would be useful to know what the underlying structure of the list is. Is it an Array, List<>, SortedList<>? What is it? I've never heard of a DList.
0
 

Author Comment

by:San24
Comment Utility
Eric,

Yes, the fastest way to do it. DList is just a simple double list. List<double>

Lets say, I have a list 0 1 2 3 4 - The altered list will look like 0 1 2 3 4 4 3 2 1. So, I have to create a new list, copy, give it a range and then concat it.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 8

Accepted Solution

by:
dericstone earned 250 total points
Comment Utility
You could also do it this way, which might be a little more efficient. But I can't think of anything else with a List<double>.
List<double> mylist;
int len = mylist.Count;
List<double> newlist = List<double>(2 * len);
for (int i = 0, j = 2 * len - 1; i < len; i++, j--)
{
  newlist[i] = newlist[j] = mylist[i];
}

Open in new window

0
 
LVL 8

Expert Comment

by:dericstone
Comment Utility
Oops, I forgot one detail. The items of the list were not allocated. I've changed it a bit.
List<double> mylist;
int len = mylist.Count;
List<double> newlist = List<double>(2 * len);
newlist.AddRange(mylist);
newlist.AddRange(mylist);
for (int i = 0, j = 2 * len - 1; i < len; i++, j--)
{
  newlist[j] = mylist[i];
}

Open in new window

0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 250 total points
Comment Utility


Similar to the first but working one solution :)

Output:

1
2
3
4
4
3
2
1
private void button2_Click(object sender, EventArgs e)
        {
            List<int> lst1 = new List<int>(new int[] { 1, 2, 3, 4 });
            List<int> lst2 = new List<int>(lst1);
            lst2.Reverse();
            lst1.AddRange(lst2);
                        foreach (int i in lst1)
            {
                System.Diagnostics.Debug.WriteLine(i.ToString());
            }

        }

Open in new window

0
 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
Also, my version with loops, might be more efficient than above my code...
private void button2_Click(object sender, EventArgs e)
        {
            List<int> lst1 = new List<int>(new int[] { 1, 2, 3, 4 });
            List<int> lst2 = new List<int>();// straight + reversed list
            for (int i = 0; i < lst1.Count * 2; i++)
            {
                int indexToAdd;
                if (i < lst1.Count)
                    indexToAdd = i;
                else
                    indexToAdd = lst1.Count*2 - 1 - i;

                lst2.Add(lst1[indexToAdd]);
            }

            // print out
            foreach (int i in lst2)
            {
                System.Diagnostics.Debug.WriteLine(i.ToString());
            }

        }

Open in new window

0
 

Author Comment

by:San24
Comment Utility
Eric and Jimbel...Thanks a lot. This is what I`m doing


            List<double> TempX = new List<double>();
            TempX.AddRange(X);
            TempX.Reverse();
            TempX.RemoveRange(XCnt - 1, 1);
            X.AddRange(TempX);
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now