# 慢时光~

## 链表常见的题型（java实现）

import java.util.*;
/**********
*@Author:Tom-shushu
*@Description:链表问题
*@Date:21:58 2019/10/2
*  .--,       .--,
* ( (  \.---./  ) )
*  '.__/o   o\__.'
*     {=  ^  =}
*      >  -  <
*     /       \
*    //       \\
*   //|   .   |\\
*   "'\       /'"_.-~^'-.
*      \  _  /--'         
*    ___)( )(___
*   (((__) (__)))    高山仰止,景行行止.虽不能至,心向往之。
*
**********/
public class Node {
public int value;
public Node next;

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

//打印链表的公共部分
} else {
}
}
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//删除单链表的倒数第K个节点
//版本一
public Node remove1(Node head, int k) {
if (head == null || k < 1) {
}
while (cur != null) {
k--;
cur = cur.next;
}
if (k == 0) {//要删除的是第一个
}
if (k < 0) {
while (++k != 0) {
cur = cur.next;
}
cur.next = cur.next.next;
}
}

//版本二
public Node remove2(Node head, int k) {
if (head == null || k <= 0) {
return null;
}
//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;
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//翻转单向链表
//版本一
Node pre = null;
Node next = null;
}
return pre;
}

//版本二
}
while (pre != null) {
Node temp = pre.next;
pre = temp;
}
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//环形约瑟夫问题
public Node yuesefu(Node head, int m) {
}
last = last.next;
}
int count = 0;
if (++count == m) {
count = 0;
} else {
last = last.next;
}
}
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//判断一个链表是否是回文链表
Stack<Node> stack = new Stack<Node>();
while (cur != null) {
stack.push(cur);
cur = cur.next;
}
return false;
}
}
return true;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//两个单链表生成相加链表
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
}
}
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;
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//删除无需单链表中重复出现的节点
return;
}
HashSet<Integer> set = new HashSet<Integer>();
while (cur != null) {
if (set.contains(cur.value)) {
pre.next = cur.next;
} else {
pre = cur;
}
cur = cur.next;
}
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//在单链表中删除指定值得节点
public Node deletevalue(Node head, int num) {
Stack<Node> stack = new Stack<Node>();
}
}
while (!stack.isEmpty()) {
}
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//合并两个有序单链表(递归)
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;
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++==
//环形链表的插入
Node node = new Node(num);
node.next = node;
return node;
}
if (pre.value <= num && cur.value >= num){
break;
}
pre = cur;
cur = cur.next;
}
pre.next = node;
node.next = cur;
}