public class ListNode<Integer> { Node<Integer> head;// Head node int size=0;// Number of nodes
public class Node<T>{ T vaule;//data value Node<T> next;// Point to the next node public Node(T
vaule, Node<T> next) {// Construction method this.vaule = vaule; this.next = next; } } public
ListNode() {} /** * Adding method of linked list * @param data */ public void add(Integer data){
if(head==null){ head=new Node<Integer>(data,null); size++; }else{ Node<Integer>
temp=head; while(temp.next!=null){ temp=temp.next; } temp.next=new
Node<Integer>(data,null); size++; } } public int size(){ if(head==null){ return
0; }else{ int i=1; Node<Integer> temp=head; while(temp.next!=null){
temp=temp.next; i++; } return i; } } /** * Node index * @param index * @return */
public Node<Integer> get(int index){ if(index>size||index<0){ throw new
IndexOutOfBoundsException(); }else{ int i=0; Node<Integer> temp=head;
if(index==i){ return temp; } while(temp.next!=null){ temp=temp.next;
if(index==++i){ return temp; } } return null; } } /** * Delete para n Nodes * @param n */
public void deleteNode(int n){ if(n>size||n<0){ throw new
IndexOutOfBoundsException(); } Node faster=head;// Pointer while(faster!=null){
if(--n==1){// The node before the deleted node faster.next=faster.next.next;// Let the pointer next point next Of next
break; } faster=faster.next; } } /** * remove according to value Value deletion * @param o * @return */
public boolean remove(Integer o) { Node<Integer> temp=head; if(temp.vaule==o){
head=null; head.next=null; size--; return true; } Node<Integer>
tempBefore;// Record the node before you want to delete the node while(temp.next!=null){ tempBefore=temp;
temp=temp.next; if(temp.vaule==o){ if(temp.next!=null){
tempBefore.next=temp.next; } size--; return true; } } return false; } /** *
Reverse linked list * Linked list in place inversion * head->1->2->3->4->5 In place inversion process of : * * head->2->1->3->4->5 *
head->3->2->1->4->5 * head->4->3->2->1->5 * head->5->4->3->2->1 */ public void
reverseNode(){ // Current node Node current = head; Node prev = null;//current Previous node ( Pioneer node )
Node temp = null; while(current != null){ // Record moves back to next node temp = current.next; //
Of the current node next Points to the previous node current.next = prew; // The previous node moves back to the current node prew = current; //
Current node moves backward current = temp; } } }
Technology