链表是面试中最常见的一种题型,因为他的每个题的代码短,短短的几行代码就可以体现出应聘者的编码能力,所以它也就成为了面试的重点。

链表常见的操作有1.打印链表的公共部分,2.删除链表的倒数第K个节点,3.翻转单向链表,4.环形约瑟夫环问题,5.判断链表是否是一个回文链表,6.两个链表生成相加链表,7.删除无序链表中重复出现的节点,8.删除指定值得节点,9.合并两个有序的单链表,10.环形链表的插入

import java.util.*;
/**********
*@Author:Tom-shushu
*@Description:链表问题
*@Date:21:58 2019/10/2
* .--, .--,
* ( ( \.---./ ) )
* '.__/o o\__.'
* {= ^ =}
* > - <
* / \
* // \\
* //| . |\\
* "'\ /'"_.-~^`'-.
* \ _ /--' `
* ___)( )(___
* (((__) (__))) 高山仰止,景行行止.虽不能至,心向往之。
*
**********/
public class Node {
public int value;
public Node head;
public Node next; public Node(int data) {
this.value = data;
} //打印链表的公共部分
public void print(Node head1, Node head2) {
while (head1 != null && head2 != null) {
if (head1.value < head2.value) {
head1 = head1.next;
} else if (head1.value > head2.value) {
head2 = head2.next;
} else {
System.out.println(head1.value);
head1 = head1.next;
head2 = head2.next;
}
}
} //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//删除单链表的倒数第K个节点
//版本一
public Node remove1(Node head, int k) {
if (head == null || k < 1) {
return head;
}
Node cur = head;
while (cur != null) {
k--;
cur = cur.next;
}
if (k == 0) {//要删除的是第一个
head = head.next;
}
if (k < 0) {
cur = head;
while (++k != 0) {
cur = cur.next;
}
cur.next = cur.next.next;
}
return head;
} //版本二
public Node remove2(Node head, int k) {
if (head == null || k <= 0) {
return null;
}
Node slow = head;
Node fast = head;
//fast 指向 k + 1
for (int i = 1; i < k + 1; i++) {
if (fast.next != null) {
fast = fast.next;
} else {
return null;
}
}
//fast指向尾部,slow指向倒数K+1,即 k 的前一个数。
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
//删除第 k 个数。
slow = slow.next.next;
return head;
} //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//翻转单向链表
//版本一
public Node reList(Node head) {
Node pre = null;
Node next = null;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
} //版本二
public Node reList2(Node head) {
if (head == null || head.next == null) {
return head;
}
Node pre = head;
Node newHead = null;
while (pre != null) {
Node temp = pre.next;
pre.next = newHead;
newHead = pre;
pre = temp;
}
return newHead;
} //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//环形约瑟夫问题
public Node yuesefu(Node head, int m) {
if (head == null || head.next == head || m < 1) {
return head;
}
Node last = head;
while (last.next != head) {
last = last.next;
}
int count = 0;
while (head != last) {
if (++count == m) {
last.next = head.next;
count = 0;
} else {
last = last.next;
}
head = last.next;
}
return head;
} //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//判断一个链表是否是回文链表
public boolean isHuiWen(Node head) {
Stack<Node> stack = new Stack<Node>();
Node cur = head;
while (cur != null) {
stack.push(cur);
cur = cur.next;
}
while (head != null) {
if (head.value != stack.pop().value) {
return false;
}
head = head.next;
}
return true;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //两个单链表生成相加链表
public Node xinagjialainbiao(Node head1, Node head2) {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
while (head1 != null) {
stack1.push(head1.value);
head1 = head1.next;
}
while (head2 != null) {
stack2.push(head1.value);
head2 = head2.next;
}
int ca = 0;
int n1 = 0;
int n2 = 0;
int n = 0;
Node node = null;
Node pre = null;
while (!stack1.isEmpty() || !stack2.isEmpty()) {
if (stack1.isEmpty()) {
n1 = 0;
} else {
n1 = stack1.pop();
}
if (stack2.isEmpty()) {
n2 = 0;
} else {
n2 = stack2.pop();
}
pre = node;
node = new Node(n % 10);
node.next = pre;
}
if (ca == 1) {
pre = node;
node = new Node(1);
node.next = pre;
}
return node;
} //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//删除无需单链表中重复出现的节点
public void deletecf(Node head) {
if (head == null) {
return;
}
HashSet<Integer> set = new HashSet<Integer>();
Node pre = head;
Node cur = head.next;
set.add(head.value);
while (cur != null) {
if (set.contains(cur.value)) {
pre.next = cur.next;
} else {
set.add(cur.value);
pre = cur;
}
cur = cur.next;
}
} //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//在单链表中删除指定值得节点
public Node deletevalue(Node head, int num) {
Stack<Node> stack = new Stack<Node>();
while (head != null) {
if (head.value != num) {
stack.push(head);
}
head = head.next;
}
while (!stack.isEmpty()) {
stack.peek().next = head;
head = stack.pop();
}
return head;
} //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//合并两个有序单链表(递归)
public Node Merge(Node list1, Node list2) {
if (list1 == null) {
return list2;
}
if (list2 == null) {
return list1;
}
if (list1.value <= list2.value) {
list1.next = Merge(list1.next, list2);
return list1;
} else {
list2.next = Merge(list1, list2.next);
return list2;
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++==
//环形链表的插入
public Node insertNum(Node head,int num){
Node node = new Node(num);
if(head == null){
node.next = node;
return node;
}
Node pre = head;
Node cur = head.next;
while (cur != head){
if (pre.value <= num && cur.value >= num){
break;
}
pre = cur;
cur = cur.next;
}
pre.next = node;
node.next = cur;
return head.value < num ? head : node;
}
}

链表常见的题型(java实现)的更多相关文章

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

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

  2. java 链表常见题目

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

  3. Java 链表常见考题总结

    首先定义自定义结点类,存储节点信息: public class Node { Node next=null; int data; public Node(int data){ this.data=da ...

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

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

  5. insertion Sort List (链表的插入排序) leecode java

    逻辑简单,代码难写,基础不劳,leecode写注释不能出现中文,太麻烦,我写了大量注释,链表问题最重要的就是你那个指针式干啥的 提交地址https://oj.leetcode.com/problems ...

  6. 常见数据结构的Java实现

    单链表的Java实现 首先参考wiki上的单链表说明,单链表每个节点包含数据和指向链表中下一个节点的指针或引用.然后看代码 import java.lang.*; public class Singl ...

  7. 常见排序的JAVA实现和性能测试

    五种常见的排序算法实现 算法描述 1.插入排序 从第一个元素开始,该元素可以认为已经被排序 取出下一个元素,在已经排序的元素序列中从后向前扫描 如果该元素(已排序)大于新元素,将该元素移到下一位置 重 ...

  8. 链表的原理及java实现

    一:单向链表基本介绍 链表是一种数据结构,和数组同级.比如,Java中我们使用的ArrayList,其实现原理是数组.而LinkedList的实现原理就是链表了.链表在进行循环遍历时效率不高,但是插入 ...

  9. 剑指Offer-25.复杂链表的复制(C++/Java)

    题目: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否则 ...

随机推荐

  1. 学会spss就能找到数据分析工作吗

     大学课堂上学习了spss,老师也讲了很多知识,但是现在准备毕业了,我做的实习工作就是用业内的数据进行最新的行业研究.现在真正需要用到spss进行分析了,我却看不懂老板给的数据和分析要求,难道这就是理 ...

  2. NLP(六) 分块、句法分析、依存分析

    内置分块器 分块:从文本中抽取短语 import nltk text = 'Lalbagh Botanical Garden is a well known botanical garden in B ...

  3. c#中的委托01

    delegate 是表示对具有特定参数列表和返回类型的方法的引用的类型. 在实例化委托时,你可以将其实例与任何具有兼容签名和返回类型的方法相关联. 你可以通过委托实例调用方法. 委托用于将方法作为参数 ...

  4. tensorflow学习笔记——自编码器及多层感知器

    1,自编码器简介 传统机器学习任务很大程度上依赖于好的特征工程,比如对数值型,日期时间型,种类型等特征的提取.特征工程往往是非常耗时耗力的,在图像,语音和视频中提取到有效的特征就更难了,工程师必须在这 ...

  5. CF 551 E GukiZ and GukiZiana

    https://codeforces.com/contest/551/problem/E 分块真强. 题意就是1.区间加,2.询问整个区间中,最远的两个x的距离. 分块,然后,每次找位子用二分找即可. ...

  6. HDU-5977 - Garden of Eden 点分治

    HDU - 5977 题意: 给定一颗树,问树上有多少节点对,节点对间包括了所有K种苹果. 思路: 点分治,对于每个节点记录从根节点到这个节点包含的所有情况,类似状压,因为K<=10.然后处理每 ...

  7. Java虚拟机原理和调优

    https://blog.csdn.net/sun1021873926/article/details/78002118 115道Java经典面试题(面中率最高.最全) 史上最全 40 道 Dubbo ...

  8. 集合数组与String的互转

    1.集合转成数组: 转之前集合里面存的什么类型的数据,就new什么类(特别:存的是基本数据的封装类,就要new他的封装类) 例如: 1.1集合: ArrayList<Character> ...

  9. 初步认识JWT

    前言: 现在越来越多的项目或多或少会用到JWT,为什么会出现使用JWT这样的场景的呢? 假设现在有一个APP,后台是分布式系统.APP的首页模块部署在上海机房的服务器上,子页面模块部署在深圳机房的服务 ...

  10. SpringBoot 2.0 + 阿里巴巴 Sentinel 动态限流实战

    前言 在从0到1构建分布式秒杀系统和打造十万博文系统中,限流是不可缺少的一个环节,在系统能承受的范围内既能减少资源开销又能防御恶意攻击. 在前面的文章中,我们使用了开源工具包 Guava 提供的限流工 ...