反转链表、
public class reverseList {
public ListNode reverseList(ListNode head){
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);
System.out.print(re.val + "");
相交链表、
import java.util.HashSet;
public class interlinkedlist {
public ListNode getIntersectionNode1(ListNode headA,ListNode headB){
public ListNode getIntersectionNode2(ListNode headA,ListNode headB){
if(headA==null||headB==null) return null;
ListNode pA=headA,pB=headB;
pA=pA==null?headB:pA.next;
pB=pB==null?headA:pB.next;
public static void main(String[] args) {
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;
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 + "'");
System.out.println("No intersection");
public ListNode(int val, ListNode next) {
回文链表、
//核心思想是通过递归的方式从链表的尾部向前进行比较,同时用一个前指针从头部向尾部进行比较
public class PalindromeLinkedList {
private ListNode frontPointer;
private boolean recursivelyCheck(ListNode currentNode) {
if (currentNode != null) {
if (!recursivelyCheck(currentNode.next)) {
if (currentNode.val != frontPointer.val) {
frontPointer = frontPointer.next;
public boolean isPalindrome(ListNode head) {
return recursivelyCheck(head);
public static void main(String[] args) {
ListNode node1 = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(2);
ListNode node4 = new ListNode(3);
PalindromeLinkedList solution = new PalindromeLinkedList();
boolean result = solution.isPalindrome(node1);
System.out.println("链表是否是回文: " + result);