首先定义自定义结点类,存储节点信息:

public class Node {

    Node next=null;
int data;
public Node(int data){
this.data=data;
}
}

获取链表长度:

private int length() {
int length=0;
Node temp=head;
while(temp!=null){
length++;
temp=temp.next;
}
return length;
}

打印链表:

public void printMyList(){
Node temp=this.head;
while(temp!=null){
System.out.print(temp.data);
temp=temp.next;
if(temp!=null){
System.out.print("-->");
}else{
System.out.println();
}
}
}

向链表中插入数据:

public void addNode(int d){
Node node=new Node(d);
if(head==null){
head=node;
return;
}
Node temp=head;
while(temp.next!=null){
temp=temp.next;
}
temp.next=node;
}

向链表中插入结点:

public void addNode(Node node){
if(head==null){
head=node;
return;
}
Node temp=head;
while(temp.next!=null){
temp=temp.next;
}
temp.next=node;
}

在链表尾部添加另一个链表:

public static void  addList(MyLinkedList list,MyLinkedList afterlist) {
Node thishead = list.head;
Node addhead = afterlist.head;
if(thishead==null){
thishead=addhead;
return;
}
Node temp=thishead;
while(temp.next!=null){
temp=temp.next;
}
temp.next=addhead;
}

从链表中删除指定位置的数据:

public boolean deleteNode(int index){//index:删除的元素的位置
if(index<1||index>length()){
return false;
}
if(index==1){
head=head.next;
return true;
}
int i=2;
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 Node orderList(){
Node nextNode=null;
int temp=0;
Node headNode=head;
while(headNode.next!=null){
nextNode=headNode.next;
while(nextNode!=null){
if(headNode.data>nextNode.data){
temp=nextNode.data;
nextNode.data=headNode.data;
headNode.data=temp;
}
nextNode=nextNode.next;
}
headNode=headNode.next;
}
return head;
}

从链表中删除重复数据 第一种方法

public void deleteRepetition1(){
Hashtable<Integer, Integer>hashtable=new Hashtable<>();
Node temp=this.head;
Node pre=null;
while(temp!=null){
if(hashtable.containsKey(temp.data))
{
pre.next=temp.next;
}
else
{
hashtable.put(temp.data, 1);
pre=temp;
}
temp=temp.next;
}
}

从链表中删除重复数据 第二种方法:

public void deleteRepetition2(){
Node temp=head;
while(temp!=null){
Node i=temp;
while(i.next!=null){
if(temp.data==i.next.data)
{
i.next=i.next.next;
}
else
{
i=i.next;
}
}
temp=temp.next;
}
}

找出单链表中的倒数第k个元素:

public Node findLastElem(int k){
if(k<1){
System.out.println("k不合法");
return null;
}
if(head==null){
System.out.println("链表不包含元素");
return null;
}
Node p1=head;
Node p2=head;
for(int i=0;i<k-1;i++){
p2=p2.next;
}
while(p2.next!=null){
p1=p1.next;
p2=p2.next;
}
return p1;
}

链表反转:

public void reversal(){
Node pReversalHead=this.head;
Node pNode=this.head;
Node pPrev=null;
while(pNode!=null){
Node pNext=pNode.next;
if(pNext==null)
{
pReversalHead=pNode;
}
pNode.next=pPrev;
pPrev=pNode;
pNode=pNext;
}
this.head=pReversalHead;
}

不反转链表,倒序输出链表元素:

public void printReversalList(Node pHead){
if(pHead.next==null){
System.out.print(pHead.data);
}
if(pHead.next!=null){
printReversalList(pHead.next);
System.out.print("-->"+pHead.data);
}
}

寻找单链表中间节点:

public Node searchMidNode(){
Node pNode=this.head;
Node qNode=this.head;
while(pNode!=null&&pNode.next!=null&&pNode.next.next!=null){
pNode=pNode.next.next;
qNode=qNode.next;
}
return qNode;
}

判断一个链表是否有环:

public boolean isHaveLoop(){
Node fast=head;
Node slow=head;
if(fast==null){
return false;
}
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow){
return true;
}
}
return false;
}

寻找环入口:

public Node getLoopStart(MyLinkedList list){
//在链表头和相遇点分别设置一个指针,每次各走一步,两个指针必定相遇,且第一个相遇点为环入口
Node slow=list.head;
Node fast=list.head;
while(slow.next!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
if(slow==fast){
break;
}
}
while(slow!=fast){
slow=slow.next;
fast=fast.next;
}
return slow;
}

在不知道头指针的情况下删除指定节点:

public boolean deleteNode(Node node){
//此方法不能删除链表最后一个节点,因为无法使其前驱结点的next指向null;
if(node==null||node.next==null){
return false;
}
node.data=node.next.data;
node.next=node.next.next;
return true;
}

判断两个链表是否相交:

public static boolean isIntersect(MyLinkedList list,MyLinkedList list2){
Node head1=list.head;
Node head2=list2.head;
if(head1==null||head2==null){
return false;
}
while(head1.next!=null){
head1=head1.next;
}
while(head2.next!=null){
head2=head2.next;
}
return head1==head2;
}

寻找两个链表相交的第一个节点:

public static Node getFirstMeetNode(MyLinkedList list,MyLinkedList list2){
Node head1=list.head;
Node head2=list2.head;
if (isIntersect(list, list2)==false){
return null;
}
Node t1=head1;
Node t2=head2;
if(list.length()>list2.length()){
int d=list.length()-list2.length();
while(d!=0){
t1=t1.next;
d--;
}
}
else {
int d=list2.length()-list.length();
while(d!=0){
t2=t2.next;
d--;
}
}
while(t1!=t2){
t1=t1.next;
t2=t2.next;
}
return t1;
}

Java 链表常见考题总结的更多相关文章

  1. java 链表常见题目

    如何判断单链表是否存在环 方法一.穷举遍历方法一:首先从头节点开始,依次遍历单链表的每一个节点.每遍历到一个新节点,就从头节点重新遍历新节点之前的所有节点,用新节点ID和此节点之前所有节点ID依次作比 ...

  2. Java链表常见操作【剑指Offer】03:从尾到头打印链表

    题目描述 输入一个链表,按链表从尾到头的顺序返回一个ArrayList. 题解一:递归 /* 在最后一次递归方法返回以后,每一层的递归方法都会做一个arrayList.add(listNode.val ...

  3. java实现单链表常见操作

    一.概述: 本文主要总结单链表常见操作的实现,包括链表结点添加.删除:链表正向遍历和反向遍历.链表排序.判断链表是否有环.是否相交.获取某一结点等. 二.概念: 链表: 一种重要的数据结构,HashM ...

  4. 链表常见的题型(java实现)

    链表是面试中最常见的一种题型,因为他的每个题的代码短,短短的几行代码就可以体现出应聘者的编码能力,所以它也就成为了面试的重点. 链表常见的操作有1.打印链表的公共部分,2.删除链表的倒数第K个节点,3 ...

  5. Java基础常见英语词汇

    Java基础常见英语词汇(共70个) ['ɔbdʒekt] ['ɔ:rientid]导向的                             ['prəʊɡræmɪŋ]编程 OO: object ...

  6. Java链表基本操作和Java.util.ArrayList

    Java链表基本操作和Java.util.ArrayList 今天做了一道<剑指offer>上的一道编程题“从尾到头打印链表”,具体要求如下:输入一个链表,按链表值从尾到头的顺序返回一个A ...

  7. Java集合类常见的问题

    本篇文章将尝试回答一些Java集合类常见的问题,以作知识梳理之用. ArrayList和LinkList以及Vector的区别 就实现方式而言: ArrayList是以数组的方式实现的列表. Link ...

  8. Java英文单词Java基础常见英语词汇

    Java英文单词Java基础常见英语词汇(共70个)                                                                          ...

  9. Java基础-JAVA中常见的数据结构介绍

    Java基础-JAVA中常见的数据结构介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是数据结构 答:数据结构是指数据存储的组织方式.大致上分为线性表.栈(Stack) ...

随机推荐

  1. 如何用一天时间实现自己的RPC框架

    前言 最近,闲来无事,自己写了一个简单的RPC框架,我把它叫做SimpleRpc.它有多简单?一共只有1400行代码.这个RPC只是作为自己试验作品,交流技术之用,当然如果你敢用,也可以放到生产环境之 ...

  2. 我的Java设计模式-工厂方法模式

    女朋友dodo闹脾气,气势汹汹的说"我要吃雪糕".笔者心里啊乐滋滋的,一支雪糕就能哄回来,不亦乐乎?! 但是,雪糕买回来了,她竟然说"不想吃雪糕了,突然想吃披萨" ...

  3. Eclipse Oxygen 解决 自动导包的问题

    换成了 Eclipse 的Oxygen 版本 , 发现之前好用的自动导包功能不能用了 (Ctrl+Shift+O) 再 网上看资料  上面说 将  In Windows 替换为Editing Java ...

  4. Java并发之CyclicBarrier、CountDownLatch、Phaser

    在Java多线程编程中,经常会需要我们控制并发流程,等其他线程执行完毕,或者分阶段执行.Java在1.5的juc中引入了CountDownLatch和CyclicBarrier,1.7中又引入了Pha ...

  5. Linux 文件查找

    在Linux系统的查找相关的命令: which 查看可执行文件的位置 whereis 查看文件的位置 locate 配合数据库查看文件位置 find 实际搜寻硬盘查询文件名称 whereis wher ...

  6. ThinkPHP中foreach和volist的区别

    1.foreach标签foreach标签用于循环输出:foreach(name,item,key)name(必须):要输出的数据模板变量item(必须):循环单原变量key(可选):循环的key变量, ...

  7. __无标题栏的窗口拖动___javafx

    遇到困难::添加mouseevent监听,我用的mouse_DragedDetected配合MouseEvent.Pressed,,闪的不行,后来借鉴swing的pressed,move,releas ...

  8. python之字典

    1.用{}创建字典 代码: 1 2 x = {"a":"1", "b":"2"} print x 输出: {'a': ' ...

  9. WPF:获取DataGrid控件单元格DataGridCell

    转载:http://blog.csdn.net/jhqin/article/details/7645357 /* ------------------------------------------- ...

  10. menu菜单项和menubutton菜单按钮的结合使用

    <!--创建需要显示的菜单按钮(munebutton),menu指定的是菜单项--><a href="javascript:void(0)" id="m ...