栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现单链表的各种操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现单链表的各种操作

主要内容:

  • 单链表的基本操作
  • 删除重复数据
  • 找到倒数第k个元素
  • 实现链表的反转
  • 从尾到头输出链表
  • 找到中间节点
  • 检测链表是否有环
  • 在不知道头指针的情况下删除指定节点
  • 如何判断两个链表是否相交并找出相交节点

直接上代码,就是这么奔放~~~

package pers.ty.$1101datastructure;
import java.util.Hashtable;

public class MylinkedList {
  Node head = null;//链表头的作用
  
  public void addNode(int d){
    Node newNode=new Node(d);
    if(head==null){
      head=newNode;
      return;
    }
    Node tmp=head;
    while(tmp.next!=null){
      tmp=tmp.next;
    }
    //add Node to end
    tmp.next=newNode;
  }
  
  public Boolean deleteNode(int index){
    if(index<1||index>length()){
      return false;//删除元素位置不合理
    }
    //删除链表中的第一个元素
    if(index==1){
      head=head.next;
      return true;
    }
    int i=1;
    Node preNode=head;
    Node curNode=preNode.next;
    while(curNode!=null){
      if(i==index){
 preNode.next=curNode.next;
 return true;
      }
      preNode=curNode;
      curNode=curNode.next;
      i++;
    }
    return true;
  }
  
  public int length(){
    int length=0;
    Node tmp=head;
    while(tmp!=null){
      length++;
      tmp=tmp.next;
    }
    return length;
  }
  
  public Node orderList(){
    Node nextNode=null;
    int temp=0;
    Node curNode=head;
    while(curNode.next!=null){
      nextNode=curNode.next;
      while(nextNode!=null){
 if(curNode.data>nextNode.data){
   temp=curNode.data;
   curNode.data=nextNode.data;
   nextNode.data=temp;
 }
 nextNode=nextNode.next;
      }
      curNode=curNode.next;
    }
    return head;
  }
  
  public void printList(){
    Node tmp=head;
    while(tmp!=null){
      System.out.print(tmp.data+" ");
      tmp=tmp.next;
    }
    System.out.println();
  }
  
  public void deleteDuplecate1(){
    Hashtable table=new Hashtable();
    Node tmp=head;
    Node pre=null;
    while (tmp!=null) {
      if(table.containsKey(tmp.data))
 pre.next=tmp.next;
      else{
 table.put(tmp.data, 1);
 pre=tmp;
      }
      tmp=tmp.next;
    }
  }
  
  public void deleteDuplecate2(){
    Node p=head;
    while (p!=null) {
      Node q=p;
      while(q.next!=null){
 if(p.data==q.next.data){
   q.next=q.next.next;
 }else{
   q=q.next;
 }
      }
      p=p.next;
    }
  }
  
  public Node findElem(Node head,int k){
    if(k<1||k>this.length())
      return null;
    Node p1=head;
    Node p2=head;
    for (int i = 0; i < k-1; i++) 
      p2=p2.next;
    while (p2.next!=null) {
      p2=p2.next;
      p1=p1.next;
    }
    return p1;
  }
  
  public void reverseIteratively(Node head){
    Node pReversedHead=head;
    Node pNode=head;
    Node pPrev=null;
    while (pNode!=null) {
      Node pNext=pNode.next;
      if(pNext==null)
 pReversedHead=pNode;
      pNode.next=pPrev;
      pPrev=pNode;
      pNode=pNext;    
    }
    this.head=pReversedHead;
  }
  
  public void printListReversely(Node head){
    if(head!=null){
      printListReversely(head.next);
      System.out.print(head.data+" ");
    }
  }
  
  public Node searchMid(Node head){
    Node q=head;
    Node p=head;
    while (p!=null&&p.next!=null&&p.next.next!=null) {
      q=q.next;
      p=p.next.next;
    }
    return q;
  }
  
  public boolean deleteNode(Node n){
    if(n==null||n.next==null)
      return false;
    int tmp=n.data;
    n.data=n.next.data;
    n.next.data=tmp;
    n.next=n.next.next;
    return true;
  }
  
  public boolean isIntersect(Node h1,Node h2){
    if(h1==null||h2==null)
      return false;
    Node tail1=h1;
    while (tail1.next!=null){ 
      tail1=tail1.next;
    }
    Node tail2=h2;
    while(tail2.next!=null){
      tail2=tail2.next;
    }
    return tail1==tail2;
  }
  
  public Node getFirstMeetNode(Node h1,Node h2){
    if(h1==null||h2==null)
      return null;
    Node tail1=h1;
    int len1=1;
    while (tail1.next!=null){ 
      tail1=tail1.next;
      len1++;
    }
    Node tail2=h2;
    int len2=1;
    while(tail2.next!=null){
      tail2=tail2.next;
      len2++;
    }
    if(tail1!=tail2){
      return null;
    }
    Node t1=h1;
    Node t2=h2;
    //找出较长的链表先遍历
    if(len1>len2){
      int d=len1-len2;
      while(d!=0){
 t1=t1.next;
 d--;
      }  
    }
    if(len1

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/148347.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号