1 链表 定义
        

2 链表代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
   | public class LinkedList<E>{     private class Node{         public E e;         public Node next;         public Node(E e,Node next){             this.e = e;             this.next = next;         }         public Node(E e){             this(e,null);         }         public Node(){             this(null,null);         }         @Override         public String toString(){             return e.toString();         }     }          private Node head;     private int size;     public LinkedList(){         head = null;         size = 0;     }          public int getSize(){         return size;     }
           public boolean isEmpty(){         return size==0;     } }
  | 
 
Node.java
1 2 3 4 5 6 7 8
   |  public class Node {             private Object data;                private Node next;           }
  | 
 

3 ⭐️Java实现链表
3.1创建链表(增加节点)
 我将head节点定义在成员变量上:
1
   | private static Node head = new Node();
   | 
 
首先,我们定义一个类作为节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   | public class Node {
           public Integer data;               public Node next;
      public Node() {     }
      public Node(int data) {         this.data = data;     }
      public Node(int data, Node next) {         this.data = data;         this.next = next;     } }
  | 
 
 创建链表(增加节点) 
   向链表中尾部插入数据:
- 找到尾节点进行插入
 
- 即使头节点.next为null,不走while循环,也是将头节点与新节点连接的(我已经将head节点初始化过了,因此没必要判断头节点是否为null)~
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   | 
 
 
 
  public static void addData(int value) {
           Node newNode = new Node(value);
           Node temp = head;
           while (temp.next != null) {         temp = temp.next;     }
           temp.next = newNode;
  }
 
  | 
 
3.2遍历链表
上面我们已经编写了增加方法,现在遍历一下看一下是否正确~~~
从首节点开始,不断往后面找,直到后面的节点没有数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
   | 
 
 
 
  public static void traverse(Node head) {
                Node temp = head.next;
      while (temp != null) {
          if (temp.data != null) {             System.out.println("关注公众号Java3y:" + temp.data);         }
                   temp = temp.next;     } }
 
  | 
 

3.3插入节点
- 插入一个节点到链表中,首先得判断这个位置是否是合法的,才能进行插入~
 
- 找到想要插入的位置的上一个节点就可以了 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
   | public void add(int index,E e){     if(index<0 || index>e)         throw new IllegalArgumentException("Index is Illegal");     Node prev = dummyHead;     for(int i=0;i<index;i++){         prev = prev.next;     }     prev.next = new Node(e,prev.next);     size++; }
  public void addFirst(E e){     add(0,e); }
  public void addLast(E e){     add(size,e); }
  | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   | public E remove(int index){     if(index<0 || index>=size)         throw new IllegalArgumentException("index is Illegal");     Node prev = dummyHead;     for(int i=0;i<index;i++){         prev = prev.next;     }     E delNode = prev.next;     prev.next = prev.next.next;      delNode.next = null;     return delNode.e; }
  public E removeFirst(){     return remove(0); }
  public E removeLast(){     return remove(size-1); }
  | 
 
5 虚拟头结点



反转链表
         
        
            
                
                    
                        Author:
                        John Doe
                    
                
                
                    
                        Permalink:
                        http://yoursite.com/2019/10/01/数据结构算法/数据结构与算法 总结笔记/3 链表/链表/
                    
                
                
                    
                        License:
                        Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
                    
                
                
                     
                         Slogan:
                         Do you believe in DESTINY?