在使用以下方法时需要定义一个LinkNode类来定义变量,new一个新对象进行调用,输出时需要定义输出方法
- public class ListNode {
- int value;
- ListNode next;//
-
- public ListNode(int value) {
- this.value = value;
- }
- public String toString(){
- return "ListNode [value=" + value+", next="+next+"]";
- }
- public String toString1(){
- return "ListNode [value=" + value+"]";
- }
- }
定义下面方法时需要提前定义头指针
public ListNode head = null;//定义头指针
1.插入,尾插法
- //插入,尾插法
- public void insert(int value){
- ListNode node = new ListNode(value);
- if(head==null){
- head=node;
- return;
- }
- ListNode index = head;
- while (index.next!=null){
- index = index.next;
- }
- index.next=node;
- }
2.插入,头插法
- //插入,头插法
- public void headinsert(int value){
- ListNode node = new ListNode(value);
- if(head==null){
- head=node;
- return;
- }
- node.next=head;
- head=node;
- }
3.输出链表上的值
- //输出链表上的值
- public void printLink1(){
- ListNode index = head;
- while (index.next!=null){//最后一个value不进入循环
- System.out.print(index.value+" ");
- index = index.next;
- }
- System.out.println(index.value);
- }
- //输出链表上的值
- public void printLink2(){
- ListNode index = head;
- while (index!=null) {//最后一个value进入循环
- System.out.print(index.value+" ");
- index = index.next;
- }
- }
4.输出链表长度
- //输出链表长度
- public int getListLength(){
- int temp=0;
- ListNode index = head;
- while (index!=null){
- temp++;
- index =index.next;
-
- }
- return temp;
- }
5.查找某个元素是否在链表上
- //查找某个元素是否在链表上
- public boolean contains(int a){
- boolean b= false;
- ListNode index =head;
- if (head==null){
- b = false;
- }
- while (index!=null){
- if (index.value == a){
- b= true;
- }else {
- b=false;
- }
- index =index.next;
-
- }
- return b;
- }
6.任意位置插入
- //任意位置插入
- public void addNodeAtIndex(int value,int position){
- //判断插入位置是否合法
- if(position<0||position>getListLength()){
- System.out.println("插入位置不合法");
- return;
- }
- if (position==0){
- headinsert(value);
- }else if (position==getListLength()){
- insert(value);
- }else {
- //创建新节点
- ListNode node = new ListNode(value);
- ListNode index = head;
- ListNode pre = null;
- //找相关位置
- int count=0;
- while (index!=null){
- if (count==position){
- node.next=index;
- pre.next=node;
- return;
- }
- pre=index;
- index=index.next;
- count++;
- }
- }
- }
7.任意位置删除
- //任意位置删除
- public void deleteNodeAtIndex(int position){
- //判断插入位置是否合法
- if(position<0||position>getListLength()-1){
- System.out.println("删除位置不合法");
- return;
- }
- //删除位置是0号位置
- if (position==0){
- head=head.next;
- return;
- }
- //删除位置不是0号位置
- ListNode index = head;
- ListNode pre = null;
- //找相关位置
- int count=0;
- while (index!=null){
- if (count==position){
- pre.next=index.next;
- return;
- }
- pre=index;
- index=index.next;
- count++;
- }
- }
8.找中间节点,只遍历一次
- //找到中间节点,只遍历一遍
- public ListNode findMiddle(){
- ListNode fast = head;
- ListNode show = head;
- while (fast!=null&&fast.next!=null){
- fast=fast.next.next;
- show=show.next;
- }
- return show;
- }
9.判断链表是否成环
- //判断链表是否成环
- public boolean hasCycle(){
- ListNode fast = head;
- ListNode show = head;
- while (fast!=null&&fast.next!=null){
- fast=fast.next.next;
- show=show.next;
- if (fast==show){
- return true;
- }
- }
- return false;
- }
10.判断成环链表的起始节点
- //判断成环链表的起始节点
- public ListNode findHasCycle(){
- ListNode fast = head;
- ListNode show = head;
- while (fast!=null&&fast.next!=null){
- fast=fast.next.next;
- show=show.next;
- if (fast==show){
- show=head;
- while (show!=fast){
- fast= fast.next;
- show= show.next;
- }
- return show;
- }
- }
- return null;
- }
11.截取单链表后k个节点
- //截取单链表后k个节点
- public ListNode endNode(int k){
- ListNode fast = head;
- ListNode show = head;
- //fast先走k步
- for (int i = 0; i < k; i++) {
- fast=fast.next;
- }
- while (fast!=null){
- show= show.next;
- fast= fast.next;
- }
- return show;
- }
12.翻转链表,在链表上直接翻转
- //翻转链表,在链表上直接翻转
- public ListNode fanzhuan(){
- ListNode index = null;
- ListNode pre = null;
- while (head!=null){
- index=head.next;
- head.next=pre;
- pre=head;
- head=index;
- }
- head=pre;
- return head;
- }