赞
踩
链表是一种数据结构
数据+节点形式
package com.demo;
public class LinkList {
//定义一个链表的头节点
ListNode head = null;//链表的长度
public int Len(){
ListNode node=head;
int length=0;
if(head==null) {
length=0;
}
while(node!=null) {
length++;
node=node.getNext();
}
return length;
}
//尾插法
public void EndInsert(int val) {
ListNode node = new ListNode(val);
//判断头节点是否为空
if(head==null) {
head=node;
return;
}
ListNode indexNode=head;
while(indexNode.getNext()!=null) {
indexNode = indexNode.getNext();
}
indexNode.setNext(node);
}
//头插法
public void start(int val) {
ListNode node = new ListNode(val);
if(head==null) {
head=node;
return;
}
node.setNext(head);
head=node;
}
//查询
public boolean cha(int val) {
ListNode node=head;
while(node!=null) {
if(node.getVal()!=val){
node=node.getNext();
}else {
return true;
}
}
return false;
}
//插入
public void add(int val,int index){
if(index>Len()) {
throw new IndexOutOfBoundsException("index位置不合法");
}
if(index==0) {
start(val);
}else if(index==Len()) {
EndInsert(val);
}else {
ListNode newNode=new ListNode(val);
ListNode tempNode=head;
ListNode preNode=null;
int position=0;
while(tempNode!=null) {
if(position==index) {
newNode.setNext(tempNode);
preNode.setNext(newNode);
return;
}
preNode=tempNode;
tempNode=tempNode.getNext();
position++;
}
}
}
//删除
public void delete(int index) {
if(index>Len()) {
throw new IndexOutOfBoundsException("index位置不合法");
}
if(index==0) {
head=head.getNext();
return;
}
ListNode tempNode=head;
ListNode preNode=null;
int position=0;
while(tempNode!=null) {
if(position==index) {
preNode.setNext(tempNode.getNext());
tempNode.setNext(null);
}
preNode=tempNode;
tempNode=tempNode.getNext();
position++;
}
}
//截取出单链表中的后k个节点
public void jie(int index) {
if(index<0||index>Len()) {
throw new IndexOutOfBoundsException("index位置不合法");
}
ListNode tempNode=head;
ListNode preNode=head;
//快的先走
while((tempNode!=null)&&index>=1) {
tempNode=tempNode.getNext();
}
while(tempNode!=null) {
tempNode=tempNode.getNext();
preNode=preNode.getNext();
}
head=preNode;
}
/**
* 判断链表是否成环
* @return
*/
public Boolean HasCycle(){
ListNode fast = head;
ListNode slow = head;
while ((fast !=null) && (fast.getNext() !=null)){
fast = fast.getNext().getNext();
slow = slow.getNext();
if(fast == slow){
return true;
}
}
return false;
}
//合并两个链表
public void mergeTwoLists(ListNode n1,ListNode n2){
ListNode newNode=new ListNode(1);
ListNode tempnew=newNode;
while(n1!=null&n2!=null) {
if(n1.getVal()<n2.getVal()) {
tempnew.setNext(n1);
n1=n1.getNext();
}else {
tempnew.setNext(n2);
n2=n2.getNext();
}
tempnew=tempnew.getNext();
}
if(n1 == null){
tempnew.setNext(n2);
}
if(n2 == null){
tempnew.setNext(n1);
}
}
//链表的反转
public ListNode fan() {
ListNode perNode=null;
ListNode nextNode=null;
while(head!=null) {
nextNode=head.getNext();
head.setNext(perNode);
perNode=head;
head=nextNode;
}
return perNode;
}
//遍历链表
public void BianLi(){
ListNode indexNode=head;
while(indexNode!=null) {
System.out.print(indexNode.getVal());
indexNode=indexNode.getNext();
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。