Java linked list implementation and reverse order of linked list

Source: Internet
Author: User

1. Linked list

linked list has a single linked list and a doubly linked list, in Java each node is a class, where the internal classes are declared, in order to facilitate the insertion and deletion, sometimes in the list of the table Head Plus Sentinel (form a Sentinel loop or a two-way loop linked list), Such insertions and deletions will not be considered when inserting deleted elements at the boundary of the situation, convenient, as for Sentinel you may feel no use, in fact he can store a list of many information, such as you each time to find an element, in the absence of Sentinel, use P! =null, using Sentinels, can store the element to be found, and when exiting, it is necessary to judge whether the Sentinel is.

2. List of the implementation and reverse order

If you want the code to be written fast, you must be aware of the status and nature of each swap insertion. When writing code, a node points to the wrong next node, resulting in a long adjustment, be sure to figure out the status of each transformation results. The following two methods are used to implement the list, the reverse order, both ideas are inserted into the idea of sorting, but the reverse of the iteration is different.

Idea one: Starting from the first N-1 element forward iteration, once inserted to the end of the list, in a doubly linked list and contains the tail node, the time complexity is: N, when the list is n squared. Because you also want to find nodes.

Idea two: A backward iteration from the 2-n node, inserted into the head node position, each time backward can use next to save the next time to insert the element, all the way, not to find the node.

The same way of thinking in different directions, to the front plug into the back, two-way thinking .

The code is as follows:

Class List<t> {private static class node<t>{private T key; Node<t> next;public Node (T key,node<t> next) {This.key=key;this.next=next;}} private int size;private node<t> head;public List () {size=0;head=null;} public int size () {return size;} public Boolean isEmpty () {if (size==0) return True;elsereturn false;} public void Insert (T key) {head=new node<t> (key,head); size++;} Public T Front () {return head.key;} Public T get (int index) {if (index>=size| | index<0) return null;int i=0; Node<t>p=head;while (I++<index) P=p.next;return P.key;} Public T Delete () {T x=head.key;head = Head.next;size--;return x;} public void Delete (T key) {Node<t>node=searchnode (key); if (node==null) return; if (size==1) head=null;else{Node    <T>pre=head;    while (Pre.next!=null&&pre.next!=node) Pre=pre.next; Pre.next=node.next;} size--;} Public node<t> Searchnode (T key) {node<t> p= head;int i=0;while (p!=null&&p.key!=key) P=p.next; return p;} public int SearchIndex (T x) {node<t> p= head;int i=0;while (p!=null&&p.key!=x) {i++;p =p.next;} if (p!=null) return i;return-1;} Public node<t> getnode (int index) {if (index<0&&index>=size) return null;int i=0; Node<t>p=head;while (I++<index) P=p.next;return p;} /** * List of the reverse order note Be sure to link nodes when the connection to the * idea one: This is more troublesome: is to put the previous element from the back to the end of the list * and to find the element node, the search process is n so the time complexity has been: N squared */public void reverse () { if (size<=1) return; int i=size-2;             Node<t>tail=getnode (size-1); while (i>=0) {node<t>e=getnode (i);                 if (e!=head) {node<t>pre=getnode (i-1);                 Tail.next=e;                 Pre.next=e.next;                 Tail=e;             Tail.next=null;             } else {tail.next=head;             Tail=head;             Head=head.next;             Tail.next=null; } i--;}} /** * Idea II: Insert from behind like the front, insert sort of thought */public void Reverse2 () {if (size<=1) return; int i=1; Node<t>pre,insertnode,next;pre=head;next=head.next;//Each header node to insert an element is the initial head node while (i++<size) {insertnode=next;if (            Next.next!=null) Next=next.next;  Pre.next=insertnode.next; Insertnode.next=head;head=insertnode;}} /* Using the value of Java to pass, is to copy a copy of the reference, not the original reference *, although pointing to the same, so that the list cannot be converted to reverse the * public void Swap (Node<t>head) {if (head.next!=null) { Node<t>p=head;swap (P.next); T key=head.key;while (P!=null) P=p.next;p.next=head;head=p.next;}} */}public class Singlelist {public static void main (String args[]) {list<integer> list=new List<integ    Er> ();    List.insert (1);    List.insert (2);    List.insert (3);    List.insert (4);    List.insert (5);    List.reverse2 ();    System.out.println (list.get (0) + "" "+list.get (1) +" "+list.get (2) +" +list.get (3) + "+list.get (4) +" "); }}



Java linked list implementation and reverse order of linked list

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.