Java 链表常见考题总结
首先定义自定义结点类,存储节点信息:
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 链表常见考题总结的更多相关文章
- java 链表常见题目
如何判断单链表是否存在环 方法一.穷举遍历方法一:首先从头节点开始,依次遍历单链表的每一个节点.每遍历到一个新节点,就从头节点重新遍历新节点之前的所有节点,用新节点ID和此节点之前所有节点ID依次作比 ...
- Java链表常见操作【剑指Offer】03:从尾到头打印链表
题目描述 输入一个链表,按链表从尾到头的顺序返回一个ArrayList. 题解一:递归 /* 在最后一次递归方法返回以后,每一层的递归方法都会做一个arrayList.add(listNode.val ...
- java实现单链表常见操作
一.概述: 本文主要总结单链表常见操作的实现,包括链表结点添加.删除:链表正向遍历和反向遍历.链表排序.判断链表是否有环.是否相交.获取某一结点等. 二.概念: 链表: 一种重要的数据结构,HashM ...
- 链表常见的题型(java实现)
链表是面试中最常见的一种题型,因为他的每个题的代码短,短短的几行代码就可以体现出应聘者的编码能力,所以它也就成为了面试的重点. 链表常见的操作有1.打印链表的公共部分,2.删除链表的倒数第K个节点,3 ...
- Java基础常见英语词汇
Java基础常见英语词汇(共70个) ['ɔbdʒekt] ['ɔ:rientid]导向的 ['prəʊɡræmɪŋ]编程 OO: object ...
- Java链表基本操作和Java.util.ArrayList
Java链表基本操作和Java.util.ArrayList 今天做了一道<剑指offer>上的一道编程题“从尾到头打印链表”,具体要求如下:输入一个链表,按链表值从尾到头的顺序返回一个A ...
- Java集合类常见的问题
本篇文章将尝试回答一些Java集合类常见的问题,以作知识梳理之用. ArrayList和LinkList以及Vector的区别 就实现方式而言: ArrayList是以数组的方式实现的列表. Link ...
- Java英文单词Java基础常见英语词汇
Java英文单词Java基础常见英语词汇(共70个) ...
- Java基础-JAVA中常见的数据结构介绍
Java基础-JAVA中常见的数据结构介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是数据结构 答:数据结构是指数据存储的组织方式.大致上分为线性表.栈(Stack) ...
随机推荐
- Jupyter(Python)中无法使用Cache原理分析
前言 最近需要在Jupyter中写一个类库,其中有一个文件实现从数据库中读取空间数据并加载为Feature对象,Feature对象是cartopy封装的geomery列表,能够方便的用于作图等.因为有 ...
- DOM中document对象的常用属性方法
每个载入浏览器的 HTML 文档都会成为 Document 对象. Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问. 属性 1 document.anchors 返 ...
- oss滤网图片音视频过滤(1)内容检测
图片音视频过滤有好多方法,我这里就不一一介绍了,这篇文章只是简单介绍一下我在项目中使用阿里云oss滤网过滤的步骤 1.所遇问题: 1.图片视频鉴别时要设置textScanRequest.setUriP ...
- 零复制(zero copy)技术
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- 移植u-boot-2012.04.01到JZ2440
开发环境:Ubuntu 12.04 开发板:JZ2440 256M NandFlash 64M SDRAM 交叉编译器:arm-linux-gcc-4.3.2 u-boot:u-boot-2012 ...
- ThreadLocal的理解与应用场景分析
对于Java ThreadLocal的理解与应用场景分析 一.对ThreadLocal理解 ThreadLocal提供一个方便的方式,可以根据不同的线程存放一些不同的特征属性,可以方便的在线程中进行存 ...
- 搭建LAMP环境示例
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- 【转】Python中实现远程调用(RPC、RMI)简单例子
远程调用使得调用远程服务器的对象.方法的方式就和调用本地对象.方法的方式差不多,因为我们通过网络编程把这些都隐藏起来了.远程调用是分布式系统的基础. 远程调用一般分为两种,远程过程调用(RPC)和远程 ...
- jquery系列教程4-事件操作全解
点击打开: jquery系列教程1-选择器全解 jquery系列教程2-style样式操作全解 jquery系列教程3-DOM操作全解 jquery系列教程4-事件操作全解 jquery系列教程5-动 ...
- JAVA继承:编译与运行的关系(编译看左边,运行看右边)
"成员变量,静态方法看左边:非静态方法:编译看左边,运行看右边." 意思是:当父类变量引用子类对象时(Fu f = new Zi();),在这个引用变量f指向的对象中,他的成员变量 ...