任何人都可以帮助我如何反转每个替代元素并将其附加到列表末尾
例如
I / P:a-1-b-2-c-3(链接列表) O / P应为:a-b-c-1-3-2
逻辑应该反转每个替代元素并将其附加到列表的末尾。
答案 0 :(得分:0)
import java.util.*;
public class LinkedListExample {
public static void main(String args[]) {
/* Linked List Declaration */
LinkedList<String> linkedlist = new LinkedList<String>();
/*add(String Element) is used for adding the elements to the linked list*/
linkedlist.add("a");
linkedlist.add("1");
linkedlist.add("b");
linkedlist.add("2");
linkedlist.add("c");
linkedlist.add("3");
/*Display Linked List Content*/
System.out.println("Linked List Content: " +linkedlist);
for(int i=1;i<linkedlist.size();i++)
{
linkedlist.addLast(linkedlist.get(i)); //Adding the node at last
linkedlist.remove(i); //Removing Node from the previous positions
}
System.out.println("Final Content: " +linkedlist);
}
}