链表操作

定义一个单链表

public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}

1.链表反转

思路:先将下一节点纪录下来,然后让当前节点指向上一节点,再将当前节点纪录下来,再让下一节点变为当前节点。

public class MainClass {
    public static void main(String[] args) throws IOException {
        ListNode a=new ListNode(1);
        a.next=new ListNode(2);
        a.next.next=new ListNode(3);
        
        
        ListNode cur=reserves(a);
        while(cur!=null){
            System.out.println(cur.val);
            cur=cur.next;
        }
    }
    
    public static ListNode reserves(ListNode a){
        ListNode pre=null;
        ListNode cur=a;
        while(cur!=null){
            ListNode next=cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
        }
        return pre;
        
    }  
}

2.删除链表某个节点

前提:链表中值都是唯一的

public class MainClass {
    public static void main(String[] args) throws IOException {
        ListNode a=new ListNode(1);
        ListNode b=new ListNode(4);
        a.next=b;
        a.next.next=new ListNode(3);
        a.next.next.next=new ListNode(6);
        
        ListNode cur=deleteNode(a,b);
        while(cur!=null){
            System.out.println(cur.val);
            cur=cur.next;
        }
    }
    
    public static ListNode deleteNode(ListNode a,ListNode b){
        if(b.next==null){
            while(a.next!=b){
                a=a.next;
            }
            a.next=null;          
        }else if(a==b){
            a=null;
        }else{
            b.val=b.next.val;
            b.next=b.next.next;
        }
        return a;
    }  
}

3.判断链表是否有环

public class MainClass {
    public static void main(String[] args) throws IOException {
        ListNode a=new ListNode(1);
        ListNode b=new ListNode(4);
        a.next=b;
        a.next.next=new ListNode(3);
        a.next.next.next=new ListNode(6);
        //a.next.next.next.next=b;
        
        boolean flag=isRing(a);
        System.out.println(flag);
    }
    
    public static boolean isRing(ListNode head){
       if(head==null)
           return false;
        
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            slow=slow.next;
            fast=fast.next.next;
            if(slow==fast){
               return true; 
            }     
        }
        return false;
    }  
}

4.倒数K个节点

public class MainClass {
    public static void main(String[] args) throws IOException {
        ListNode a=new ListNode(1);
        ListNode b=new ListNode(4);
        a.next=b;
        a.next.next=new ListNode(3);
        a.next.next.next=new ListNode(6);
        a.next.next.next.next=new ListNode(7);;
        
        ListNode k=getReverNode(a,5);
        System.out.println(k.val);
    }
    
    public static ListNode getReverNode(ListNode head,int k){
     
        if(head==null ){
          return null;  
        }
        
        ListNode fast=head;
        for(int i=0;i<k-1;i++){
            fast=fast.next;
        }
        ListNode slow=head;
        while(fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }  
}
原文地址:https://www.cnblogs.com/hanxiaomin/p/11420583.html