3

I need to understand simple example

 LinkedList list = new LinkedList();
        list.add("J");
        list.add("A");
        list.add("V");
        list.add("A");

I have simple LinkedList and need pass it to method reverse

public void reverse(LinkedList list) {

}

which will return reversed new list

and I do not need any ArraysUtils for reversing it

what is the short and practice way for having reversed output.

The same needed understand also for simple arrays.

1
  • 1
    Naively, the way I would do it is to create a temporary copy of the list, delete the contents of the parameter and then iterate over the temporary backwards and add it to the parameter list. EDIT: I assume you will return the list by modifying the input parameter to the method reverse. This is a side effect that I don't think many would use in Java though, that's a different discussion however. Commented May 15, 2013 at 7:47

6 Answers 6

7

You can use the Collections class.

Collections.reverse(list);

Your list will be reversed after that operation.

Sign up to request clarification or add additional context in comments.

1 Comment

I know, but i need without Collectons and utils:)) how reverse given list and array by hand?
3
public void reverse(LinkedList list) {
    //run till middle and swap start element with end element and so on
    for(int i = 0, mid = list.size()/2, j = list.size() - 1; i < mid; i++, j--)
        list.set(i, list.set(j, list.get(i)));//swap
}

Note: List.set(..,..) method returns the element previously at the specified position

1 Comment

The downside of this approach is that list.set and list.get are very inefficient in LinkedList, since it needs to traverse up to half the list node by node to access each element. This implementation has time complexity of O(n ^ 2).
1

try this.

public LinkedList<String> reverse(LinkedList list) {
LinkedList reverseList=null;

for(int i=list.size();i>0;i--)
{
reverseList.add(list.get(i))
}
return reverseList;
}

Comments

1

reverse method of Collections class is what you need i guess. You can change the implementation according to your need

/**
 * Reverses the order of the elements in the specified list.<p>
 *
 * This method runs in linear time.
 *
 * @param  list the list whose elements are to be reversed.
 * @throws UnsupportedOperationException if the specified list or
 *         its list-iterator does not support the <tt>set</tt> operation.
 */
public static void reverse(List<?> list) {
    int size = list.size();
    if (size < REVERSE_THRESHOLD || list instanceof RandomAccess) {
        for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--)
            swap(list, i, j);
    } else {
        ListIterator fwd = list.listIterator();
        ListIterator rev = list.listIterator(size);
        for (int i=0, mid=list.size()>>1; i<mid; i++) {
    Object tmp = fwd.next();
            fwd.set(rev.previous());
            rev.set(tmp);
        }
    }
}

Comments

1
public ListNode reverseList(ListNode head) {
    if(head == null) return head;

    Stack<ListNode> stack = new Stack<ListNode>();
    while(head != null) {
        stack.push(head);
        head = head.next;
    }

    ListNode dummy = new ListNode(0);
    head = dummy;
    while(!stack.isEmpty()) {
        ListNode current = stack.pop();
        head.next = new ListNode(current.val);
        head = head.next;
    }

    return dummy.next;
}

Comments

0

This should be a fairly efficient solution that iterates through the list forward and backward at the same time, swapping each corresponding pair of elements.

public <E> void reverse(LinkedList<E> list) {
    List<E> firstHalf = list.subList(0, list.size() / 2);
    List<E> secondHalf = list.subList(list.size() / 2, list.size());

    for (ListIterator<E> i = firstHalf.listIterator(),
         j = secondHalf.listIterator(secondHalf.size()); i.hasNext(); ) {
        E e1 = i.next();
        E e2 = j.previous();
        i.set(e2);
        j.set(e1);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.