package com.company.a.b.al;
import java.util.LinkedList;
import java.util.Stack;
public class Solution02 {
// 定义当前节点和头节点
public Node head;
public Node current;
// 向链表中添加数据
public void add(int data){
if(head == null){
// 如果头节点为空,则将当前节点指向头节点
head = new Node(data);
current = head;
}else{
current.next = new Node(data);
current = current.next;
}
}
// 遍历链表
public void print(Node node){
if(node == null){
return ;
}else{
current = node;
while (current != null){
System.out.print(current.data);
current = current.next;
}
}
}
// 获取单链表中节点的个数
public int getLength(Node head){
// 如果头节点为空则返回0
if(head == null)
return 0;
int len = 0;
Node current = head;
while(current != null){
len++;
current = current.next;
}
return len;
}
public int findLastNode(int index){
// 返回倒数第K个元素
int length = getLength(head);
if(length == 0)
return 0;
current = head;
for(int i = 0;i<length-index;i++){
current = current.next;
}
return current.data;
}
// public Node findLastNode(Node node,int index){
对上述方法的改进:设置两个指针,同时指向第一个元素,然后将second指针向后移动K-1步,之后同时移动两个指针,直到second指针指向链表的末尾
// Node first = head;
// Node second = head;
将second结点向后移动index个位置
// for(int i = 0;i<index;i++){
// second = second.next;
// }
// while(second!=null){
// first = first.next;
// second = second.next;
// }
// return first;
// }
public Node findLastNode(Node node,int index){
// 最终版,考虑k大于链表长度的情况
if(index == 0||head == null)
return null;
Node first = head;
Node second = head;
for(int i = 0;i<index;i++){
second = second.next;
if(second == null)
return null;
}
while(second != null){
first = first.next;
second = second.next;
}
return first;
}
public Node FindMidNode(Node head){
// 在不允许计算链表长度的情况下找到链表的中间结点
// 与上面一样同时设置两个指针,first每走一步,second走两步,当second走到链表末尾时,first指向了链表的中间结点
if(head == null){
return null;
}
Node first = head;
Node second = head;
while(second != null&&second.next != null){
first = first.next;
second = second.next.next;
}
return first;
}
public Node MergeLinkList(Node head1,Node head2){
// 合并两个有序链表
// 注意分情况讨论:两个链表都为空,其中一个链表为空,两个链表都不为空几种情况,联想二路归并排序
if(head1 == null&&head2 == null)
return null;
if(head1 == null)
return head2;
if(head2 == null)
return head1;
Node head;
Node current;
// 让current指向较小的数据
if(head1.data<head2.data){
head = head1;
current = head1;
head1 = head1.next;//head1指针后移
}else{
head = head2;
current = head2;
head2 = head2.next;
}
while(head1!=null&&head2!=null){
if(head1.data<head2.data){
current.next = head1;
current = current.next;
head1 = head1.next;
}else{
current.next = head2;
current = current.next;
head2 = head2.next;
}
}
// 将有剩余的链表中的元素直接添加进去
if(head1 != null){
current.next = head1;
// current = current.next;
// head1 = head1.next;
}
if(head2 != null){
current.next = head2;
// current = current.next;
// head2 = head2.next;
}
return head;
}
//单链表反转
public Node reverseList(Node head){
// 首先考虑链表为空或只有一个结点的时候,将摘下的结点放到新链表的的最前端,即头节点的前面,并将新放入的结点设置为头节点
if(head == null||head.next == null)
return head;
Node reverseHead = null;
Node current = head;
Node next = null;
while(current != null){
next = current.next;
current.next = reverseHead;//将current的指针指向新链表的头节点
reverseHead = current;//将头结点设置为current结点
current = next;//指针后移
}
return head;
}
// 从尾到头打印单链表
public void reversePrint(Node head){
if(head == null){
return;
}
Stack<Node> stack = new Stack<>();
Node current = head;
while(current!=null){//只要结点非空,就将当前节点压栈
stack.push(current);
current = current.next;
}
//循环栈,打印结点元素
while(stack.size()>0){
System.out.print(stack.pop().data);
}
}
// 判断单链表是否有环
public boolean hasCircle(Node head){
// 可以设置快慢指针,如果链表有环,则两个指针必然相遇
if(head == null)
return false;
Node first = head;
Node second = head;
// first每走一步,second走两步
while (second != null&&second.next != null){
first = first.next;
second = second.next.next;
if(first == second)
return true;
}
return false;
}
class Node{
int data;
Node next;
public Node(int data){
this.data = data;
}
}
public static void main(String[] args) {
Solution02 list = new Solution02();
for(int i = 0;i<10;i++){
list.add(i);
}
list.print(list.head);
}
}