Single-linked list in-place reverse (Java edition)

Source: Internet
Author: User

Title: There is a linear table (A1,a2,a3,..., an), using the single-linked table L storage with the lead node, an algorithm is designed to reverse its position in place, and the linear table becomes (an,... a3,a2,a1). The so-called "in-place" refers to the secondary storage space as O (1).


Problem Solving Ideas:
If it is sequential storage, we can easily think of a solution, using 1 auxiliary variables to exchange the 1th element with the nth element, and then use this auxiliary variable to let the 2nd element exchange with the n-1 element, ... Finally, this auxiliary variable is used to exchange the N/2 element with the N+1-N/2 element.


If "in-place" is not required, you can create an auxiliary array of n elements, access each element in a single linked list once, store it in the array, and then access each element in a single-linked list, starting at the end of the array, assigning values to the elements in the single-linked list, The value of the 1th element of the array is assigned to the last element of the single-linked list.


If the single-linked list is empty or only the head node in a single-linked list, then the single-linked list does not need to be reversed, and if there is only one element in the single-linked list, its position will not change after the reverse, so it can not be reversed. When there are 2 or two or more elements in a single-linked list, it is broken from the 1th element, so that its next is empty, then the 2nd element is accessed to the nth element, and when one of the elements is accessed, it is inserted into the head node after inserting it into the 1th position. The original 1th element is then inserted in front of the n-1 element, and the original 2nd element is inserted into the front of the n-2 element, ... Until the original nth element is inserted into a 1th position. This enables the in-place inverse of the single-linked list of lead nodes.


ADT Definition:

Single-linked list of node class lnode{//to simplify access to a single-linked list, the data entry in the node is set to Publicpublic int data;public lnode next;}

Algorithm implementation:

public class Linklistutli{public static void reverse (Lnode L) {//single-linked list is empty or only a header node or only one element, without an inverse operation if (l==null| | l.next==null| | L.next.next==null) return; Lnode p=l.next.next;//P points to the 2nd element in the linear table A2 l.next.next=null;//order the 1th element in the linear table A1 the next is empty while (p!=null) {Lnode q=p.next;// P.next=l.next after inserting p into the head node; l.next=p;p=q;//continue access to next element}}}


Single-linked list in-place reverse (Java edition)

Related Article

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.