"Java" implements a recursive and reverse, which can flip a table over. __java

Source: Internet
Author: User

"Java" implements a recursive and reverse, which can flip a table over. Do not create a new table entry in the operation, just use an existing item. (table uses linkedlist<integer>)


1. Code implementation


Package Devin.wu.readbook;

Import java.util.LinkedList; public class Reversetools {public static void reverse (linkedlist<integer> list, int i) {int size = List.size (
		);

			if (I < SIZE/2) {Swapitembyindex (list, I, size-1-i);
		Reverse (list, i + 1);
		The public static void reverse (linkedlist<integer> list) {int size = List.size ();
		for (int i = 0; i < SIZE/2 i++) {swapitembyindex (list, I, size-1-i); } private static void Swapitembyindex (linkedlist<integer> list, int i, int j) {List.set (I, List.get (i) + L
		Ist.get (j));
		List.set (J, List.get (i)-List.get (j));
	List.set (i, List.get (i)-List.get (j));
		public static void Main (string[] args) {linkedlist<integer> list = new linkedlist<integer> ();
		List.add (1);
		List.add (2);
		List.add (3);
		List.add (4);

		List.add (5);
		System.out.println ("-------------to print the initial linked list.--------------------");
		
		Printlinkedlist (list); SysTem.out.println ("-------------after reversing by recursion.--------------------");
		Reverse (list);

		Printlinkedlist (list);
		System.out.println ("-------------after reversing by iteration.--------------------");
		Reverse (list, 0);

	Printlinkedlist (list); private static void Printlinkedlist (Linkedlist<integer> list) {for (Integer element:list) {System.ou
		T.PRINTLN (Element);
 }
	}

}

2. Operation Effect


-------------to print the initial linked list.--------------------
1
2
3
4
5
-------------after reversing by recursion.--------------------
5
4
3
2
1
-------------after reversing by iteration.--------------------
1
2
3
4
5



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.