力扣linkedlist

反转链表、
java 复制代码
public class reverseList {
//    1->2->3->o  、   o<-1<-2<-3
    public ListNode reverseList(ListNode head){//反转链表
        ListNode prev=null;
        ListNode curr=head;
        while(curr!=null){
            ListNode next=curr.next;
            curr.next=prev;
            prev=curr;
            curr=next;
        }
        return prev;
    }
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        reverseList solution = new reverseList();
        ListNode re = solution.reverseList(head);
        while (re != null) {
            System.out.print(re.val + "");
            re = re.next;
        }
    }
}
相交链表、
java 复制代码
import java.util.HashSet;
import java.util.Set;
public class interlinkedlist {
    public ListNode getIntersectionNode1(ListNode headA,ListNode headB){
        Set<ListNode>set=new HashSet<>();
        while(headA!=null){
            set.add(headA.next);
            headA=headA.next;
        }
        while(headB!=null){
            if(set.contains(headB)){
                return headB;
            }
            headB=headB.next;
        }
        return null;
    }
    public ListNode getIntersectionNode2(ListNode headA,ListNode headB){
        if(headA==null||headB==null) return null;
        ListNode pA=headA,pB=headB;
        while(pA!=pB){
            pA=pA==null?headB:pA.next;
            pB=pB==null?headA:pB.next;
        }
        return pA;
    }
    // 测试代码
    public static void main(String[] args) {
        // 创建两个链表
        // 链表 A: 4 -> 1 -> 8 -> 4 -> 5
        // 链表 B: 5 -> 6 -> 1 -> 8 -> 4 -> 5
        ListNode headA = new ListNode(4);
        headA.next = new ListNode(1);
        headA.next.next = new ListNode(8);
        headA.next.next.next = new ListNode(4);
        headA.next.next.next.next = new ListNode(5);
        ListNode headB = new ListNode(5);
        headB.next = new ListNode(6);
        headB.next.next = new ListNode(1);
        headB.next.next.next = headA.next.next; // 相交节点 8
        headB.next.next.next.next =headA.next.next.next;
        headB.next.next.next.next.next =headA.next.next.next.next;
        interlinkedlist solution = new interlinkedlist();
        ListNode intersection = solution.getIntersectionNode1(headA, headB);
        if (intersection != null) {
            System.out.println("Intersected at '" + intersection.val + "'");
        } else {
            System.out.println("No intersection");
        }
    }
}
class ListNode {
    int val;
    ListNode next;
    public ListNode() {
    }
    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
    ListNode(int x) {
        this.val = x;
        this.next = null;
    }
}
回文链表、
复制代码
//核心思想是通过递归的方式从链表的尾部向前进行比较,同时用一个前指针从头部向尾部进行比较
java 复制代码
package org.example;
public class PalindromeLinkedList {
    private ListNode frontPointer;
    private boolean recursivelyCheck(ListNode currentNode) {
        if (currentNode != null) {
            if (!recursivelyCheck(currentNode.next)) {
                return false;
            }
            if (currentNode.val != frontPointer.val) {
                return false;
            }
            frontPointer = frontPointer.next;
        }
        return true;
    }
    public boolean isPalindrome(ListNode head) {
        frontPointer = head;
        return recursivelyCheck(head);
    }
    public static void main(String[] args) {
        // 创建链表 1 -> 2 -> 2 -> 1
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(2);
        ListNode node4 = new ListNode(3);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        PalindromeLinkedList solution = new PalindromeLinkedList();
        boolean result = solution.isPalindrome(node1);
        System.out.println("链表是否是回文: " + result);
    }
}
相关推荐
测开小菜鸟1 小时前
使用python向钉钉群聊发送消息
java·python·钉钉
好奇龙猫2 小时前
【学习AI-相关路程-mnist手写数字分类-win-硬件:windows-自我学习AI-实验步骤-全连接神经网络(BPnetwork)-操作流程(3) 】
人工智能·算法
P.H. Infinity2 小时前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天2 小时前
java的threadlocal为何内存泄漏
java
sp_fyf_20242 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-11-01
人工智能·深度学习·神经网络·算法·机器学习·语言模型·数据挖掘
caridle2 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
^velpro^3 小时前
数据库连接池的创建
java·开发语言·数据库
苹果醋33 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
秋の花3 小时前
【JAVA基础】Java集合基础
java·开发语言·windows
香菜大丸3 小时前
链表的归并排序
数据结构·算法·链表