• 力扣linkedlist


    反转链表、
    1. public class reverseList {
    2. // 1->2->3->o 、 o<-1<-2<-3
    3. public ListNode reverseList(ListNode head){//反转链表
    4. ListNode prev=null;
    5. ListNode curr=head;
    6. while(curr!=null){
    7. ListNode next=curr.next;
    8. curr.next=prev;
    9. prev=curr;
    10. curr=next;
    11. }
    12. return prev;
    13. }
    14. public static void main(String[] args) {
    15. ListNode head = new ListNode(1);
    16. head.next = new ListNode(2);
    17. head.next.next = new ListNode(3);
    18. head.next.next.next = new ListNode(4);
    19. head.next.next.next.next = new ListNode(5);
    20. reverseList solution = new reverseList();
    21. ListNode re = solution.reverseList(head);
    22. while (re != null) {
    23. System.out.print(re.val + "");
    24. re = re.next;
    25. }
    26. }
    27. }
    相交链表、
    1. import java.util.HashSet;
    2. import java.util.Set;
    3. public class interlinkedlist {
    4. public ListNode getIntersectionNode1(ListNode headA,ListNode headB){
    5. Setset=new HashSet<>();
    6. while(headA!=null){
    7. set.add(headA.next);
    8. headA=headA.next;
    9. }
    10. while(headB!=null){
    11. if(set.contains(headB)){
    12. return headB;
    13. }
    14. headB=headB.next;
    15. }
    16. return null;
    17. }
    18. public ListNode getIntersectionNode2(ListNode headA,ListNode headB){
    19. if(headA==null||headB==null) return null;
    20. ListNode pA=headA,pB=headB;
    21. while(pA!=pB){
    22. pA=pA==null?headB:pA.next;
    23. pB=pB==null?headA:pB.next;
    24. }
    25. return pA;
    26. }
    27. // 测试代码
    28. public static void main(String[] args) {
    29. // 创建两个链表
    30. // 链表 A: 4 -> 1 -> 8 -> 4 -> 5
    31. // 链表 B: 5 -> 6 -> 1 -> 8 -> 4 -> 5
    32. ListNode headA = new ListNode(4);
    33. headA.next = new ListNode(1);
    34. headA.next.next = new ListNode(8);
    35. headA.next.next.next = new ListNode(4);
    36. headA.next.next.next.next = new ListNode(5);
    37. ListNode headB = new ListNode(5);
    38. headB.next = new ListNode(6);
    39. headB.next.next = new ListNode(1);
    40. headB.next.next.next = headA.next.next; // 相交节点 8
    41. headB.next.next.next.next =headA.next.next.next;
    42. headB.next.next.next.next.next =headA.next.next.next.next;
    43. interlinkedlist solution = new interlinkedlist();
    44. ListNode intersection = solution.getIntersectionNode1(headA, headB);
    45. if (intersection != null) {
    46. System.out.println("Intersected at '" + intersection.val + "'");
    47. } else {
    48. System.out.println("No intersection");
    49. }
    50. }
    51. }
    52. class ListNode {
    53. int val;
    54. ListNode next;
    55. public ListNode() {
    56. }
    57. public ListNode(int val, ListNode next) {
    58. this.val = val;
    59. this.next = next;
    60. }
    61. ListNode(int x) {
    62. this.val = x;
    63. this.next = null;
    64. }
    65. }
    回文链表、
    //核心思想是通过递归的方式从链表的尾部向前进行比较,同时用一个前指针从头部向尾部进行比较
    1. package org.example;
    2. public class PalindromeLinkedList {
    3. private ListNode frontPointer;
    4. private boolean recursivelyCheck(ListNode currentNode) {
    5. if (currentNode != null) {
    6. if (!recursivelyCheck(currentNode.next)) {
    7. return false;
    8. }
    9. if (currentNode.val != frontPointer.val) {
    10. return false;
    11. }
    12. frontPointer = frontPointer.next;
    13. }
    14. return true;
    15. }
    16. public boolean isPalindrome(ListNode head) {
    17. frontPointer = head;
    18. return recursivelyCheck(head);
    19. }
    20. public static void main(String[] args) {
    21. // 创建链表 1 -> 2 -> 2 -> 1
    22. ListNode node1 = new ListNode(1);
    23. ListNode node2 = new ListNode(2);
    24. ListNode node3 = new ListNode(2);
    25. ListNode node4 = new ListNode(3);
    26. node1.next = node2;
    27. node2.next = node3;
    28. node3.next = node4;
    29. PalindromeLinkedList solution = new PalindromeLinkedList();
    30. boolean result = solution.isPalindrome(node1);
    31. System.out.println("链表是否是回文: " + result);
    32. }
    33. }

  • 相关阅读:
    【吴恩达机器学习笔记】八、应用机器学习的建议
    使用 ProcessBuilder API 优化你的流程
    Linux基础命令
    常见的框架漏洞
    Minecraft HMCL 使用认证服务器LittleSkin进行登录
    【Nginx】在Linux上如何安装Nginx教程+Nginx基本命令的使用
    最全总结:这几种 Python 命令行参数化的方式真香啊
    如何提交一个PR?【OpenHarmony成长计划】【OpenHarmony开源社区】
    Vue组件的继承用法
    本地部署Perplexity 克隆:使用Llama 3.1实现高效搜索
  • 原文地址:https://blog.csdn.net/qq_58578599/article/details/139310571