1. package y2019.Algorithm.LinkedList.medium;
  2.  
  3. import y2019.Algorithm.LinkedList.ListNode;
  4.  
  5. /**
  6. * @ProjectName: cutter-point
  7. * @Package: y2019.Algorithm.LinkedList.medium
  8. * @ClassName: AddTwoNumbers
  9. * @Author: xiaof
  10. * @Description: TODO 2. Add Two Numbers
  11. * You are given two non-empty linked lists representing two non-negative integers.
  12. * The digits are stored in reverse order and each of their nodes contain a single digit.
  13. * Add the two numbers and return it as a linked list.
  14. * You may assume the two numbers do not contain any leading zero, except the number 0 itself.
  15. *
  16. * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
  17. * Output: 7 -> 0 -> 8
  18. * Explanation: 342 + 465 = 807.
  19. * @Date: 2019/8/1 9:06
  20. * @Version: 1.0
  21. */
  22. public class AddTwoNumbers {
  23.  
  24. public ListNode solution(ListNode l1, ListNode l2) {
  25. //两数相加,用链表遍历相加进位即可
  26. int increment = 0;
  27. ListNode p1 = l1, p2 = l2, tail1 = p1, tail2 = p2;
  28. while (p1 != null && p2 != null) {
  29. int curValue = p1.val + p2.val + increment;
  30. increment = curValue / 10;
  31. curValue = curValue % 10;
  32. p1.val = curValue;
  33.  
  34. tail1 = p1;
  35. tail2 = p2;
  36. p1 = p1.next;
  37. p2 = p2.next;
  38. }
  39.  
  40. //如果l1比较长
  41. if (p2 == null && p1 != null) {
  42. //吧剩下的加入链表
  43. while (p1 != null) {
  44. int curValue = p1.val + increment;
  45. increment = curValue / 10;
  46. curValue = curValue % 10;
  47. p1.val = curValue;
  48.  
  49. tail1 = p1;
  50. p1 = p1.next;
  51. }
  52. }
  53.  
  54. if (p1 == null && p2 != null) {
  55. //吧剩下的加入链表
  56. while (p2 != null) {
  57. int curValue = p2.val + increment;
  58. increment = curValue / 10;
  59. curValue = curValue % 10;
  60. tail1.next = new ListNode(curValue);
  61.  
  62. tail1 = tail1.next;
  63. p2 = p2.next;
  64. }
  65. }
  66.  
  67. //最后添加increment节点
  68. if (increment > 0) {
  69. tail1.next = new ListNode(increment);
  70. }
  71.  
  72. return l1;
  73.  
  74. }
  75.  
  76. public static void main(String[] args) {
  77. ListNode node1 = new ListNode();
  78. node1.val = 9;
  79. ListNode node2 = new ListNode();
  80. node2.val = 9;
  81.  
  82. node1.next = node2;
  83.  
  84. ListNode node21 = new ListNode();
  85.  
  86. AddTwoNumbers fuc = new AddTwoNumbers();
  87.  
  88. fuc.solution(node1, node21);
  89.  
  90. }
  91. }
  1. package y2019.Algorithm.LinkedList.medium;
  2.  
  3. import y2019.Algorithm.LinkedList.Node;
  4.  
  5. /**
  6. * @ProjectName: cutter-point
  7. * @Package: y2019.Algorithm.LinkedList.medium
  8. * @ClassName: CopyRandomList
  9. * @Author: xiaof
  10. * @Description: TODO 138. Copy List with Random Pointer
  11. * A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
  12. * Return a deep copy of the list.
  13. *
  14. * Input:
  15. * {"$id":"1","next":{"$id":"2","next":null,"random":{"$ref":"2"},"val":2},"random":{"$ref":"2"},"val":1}
  16. *
  17. * Explanation:
  18. * Node 1's value is 1, both of its next and random pointer points to Node 2.
  19. * Node 2's value is 2, its next pointer points to null and its random pointer points to itself.
  20. *
  21. * 思路,参考大神操作
  22. * 1.首先吧链表复制,但是不拆开,复制出来的链表还是连在原来的链表上
  23. * 2.根据上一个节点是下一个节点的原始复制对象,那么只需要吧random相应指向的节点的下一个
  24. * 3.拆开复制的链表
  25. * @Date: 2019/8/1 9:35
  26. * @Version: 1.0
  27. */
  28. public class CopyRandomList {
  29.  
  30. public Node solution(Node head) {
  31. if (head == null) return null;
  32. //开始复制
  33. Node p = head, p2, newHead, copy;
  34. //1 step
  35. while (p != null) {
  36. //新节点指向原来的下一个节点
  37. Node temp = new Node(p.val, p.next, p.random);
  38. //原来旧节点指向新节点
  39. p.next = temp;
  40. //移动到下一个旧节点
  41. p = temp.next;
  42. }
  43.  
  44. //2 step 修改random指向
  45. p = head;
  46. while (p != null) {
  47. p2 = p.next;
  48. if(p2.random != null) {
  49. p2.random = p.random.next;//指向复制的节点
  50.  
  51. }
  52. p = p.next.next;
  53. }
  54.  
  55. //3. step拆分链表
  56. p = head;
  57. Node nh = new Node(), np1 = nh, np2 = np1;
  58. while (p != null) {
  59. p2 = p.next.next;
  60.  
  61. np1 = p.next;
  62. np2.next = np1;
  63. np2 = np1;
  64.  
  65. p.next = p2;
  66. p = p2;
  67. }
  68.  
  69. return nh.next;
  70.  
  71. }
  72. }
  1. package y2019.Algorithm.LinkedList.medium;
  2.  
  3. import y2019.Algorithm.LinkedList.ListNode;
  4.  
  5. import java.util.HashMap;
  6. import java.util.Map;
  7.  
  8. /**
  9. * @ProjectName: cutter-point
  10. * @Package: y2019.Algorithm.LinkedList.medium
  11. * @ClassName: DetectCycle
  12. * @Author: xiaof
  13. * @Description: TODO 142. Linked List Cycle II
  14. * Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
  15. * To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked
  16. * list where tail connects to. If pos is -1, then there is no cycle in the linked list.
  17. *
  18. * Input: head = [3,2,0,-4], pos = 1
  19. * Output: tail connects to node index 1
  20. * Explanation: There is a cycle in the linked list, where tail connects to the second node.
  21. * @Date: 2019/8/1 8:57
  22. * @Version: 1.0
  23. */
  24. public class DetectCycle {
  25.  
  26. public ListNode solution(ListNode head) {
  27.  
  28. //针对有换问题,可以考虑用set,或者用2个指针,如果要找位置的话,感觉hashmap可能更好
  29. Map nodeMap = new HashMap();
  30. ListNode res = null, p = head;
  31. int index = 0;
  32. //遍历链表,直到遇到重复
  33. while (p != null) {
  34. //遍历获取下一个数据
  35. if(nodeMap.containsKey(p)) {
  36. //如果已经存在
  37. res = p;
  38. break;
  39. } else {
  40. //如果不包含
  41. nodeMap.put(p, index++);
  42. p = p.next;
  43. }
  44. }
  45.  
  46. return res;
  47. }
  48. }
  1. package y2019.Algorithm.LinkedList.hard;
  2.  
  3. import y2019.Algorithm.LinkedList.ListNode;
  4.  
  5. /**
  6. * @ProjectName: cutter-point
  7. * @Package: y2019.Algorithm.LinkedList.hard
  8. * @ClassName: MergeKLists
  9. * @Author: xiaof
  10. * @Description: 23. Merge k Sorted Lists
  11. * Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
  12. * Input:
  13. * [
  14. * 1->4->5,
  15. * 1->3->4,
  16. * 2->6
  17. * ]
  18. * Output: 1->1->2->3->4->4->5->6
  19. *
  20. * 参考: 148. Sort List的解法
  21. * @Date: 2019/8/1 10:28
  22. * @Version: 1.0
  23. */
  24. public class MergeKLists {
  25.  
  26. public ListNode mergeKLists(ListNode[] lists) {
  27. //多链表合并,可以简化为多个双链表合并,做循环即可
  28. if (lists == null || lists.length == 0) {
  29. return null;
  30. }
  31.  
  32. if (lists.length == 1) {
  33. return lists[0];
  34. }
  35. //1,先取一个数组,然后循环遍历所有数组跟之前的数组做merge
  36. ListNode res = lists[0];
  37. for(int i = 1; i < lists.length; ++i) {
  38. res = merge(res, lists[i]);
  39. }
  40.  
  41. return res;
  42. }
  43.  
  44. ListNode merge(ListNode l1, ListNode l2) {
  45.  
  46. if(l1 == null && l2 == null) {
  47. return null;
  48. } else if (l1 == null) {
  49. return l2;
  50. } else if (l2 == null) {
  51. return l1;
  52. }
  53.  
  54. //合并俩个链表
  55. ListNode newl = new ListNode(0);
  56.  
  57. //遍历两个链表,获取对应的位置添加到链表
  58. //1。老规矩,获取前一个链表位置,后一个位置,当前位置
  59. ListNode addl = newl, l1next = l1.next, l2next = l2.next, temp;
  60. //2.吧需要添加进去的链表后面加上当前节点
  61. while (l1 != null && l2 != null) {
  62. //3.把当前节点的下一个索引断开
  63. if(l1.val < l2.val) {
  64. //如果是必第二个链表的小,那么我们用第一个链表
  65. addl.next = l1;
  66. temp = l1.next;
  67. l1.next = null;
  68. l1 = temp;
  69. } else {
  70. addl.next = l2;
  71. temp = l2.next;
  72. l2.next = null;
  73. l2 = temp;
  74. }
  75. //4.吧当前索引指向下一个节点
  76. addl = addl.next;
  77. }
  78.  
  79. //5.最后吧剩余的节点直接加入到新链表中
  80. if(l1 != null) {
  81. addl.next = l1;
  82. }
  83.  
  84. if(l2 != null) {
  85. addl.next = l2;
  86. }
  87.  
  88. return newl.next;
  89. }
  90. }

【LEETCODE】64、链表分类,medium&hard级别,题目:2,138,142,23的更多相关文章

  1. LeetCode:颜色分类【75】

    LeetCode:颜色分类[75] 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 ...

  2. LeetCode 75. 颜色分类(Sort Colors) 30

    75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...

  3. LeetCode之链表总结

    链表提供了高效的节点重排能力,以及顺序性的节点访问方式,并且可以通过增删节点来灵活地调整链表的长度.作为一种常用的数据结构,链表内置在很多高级编程语言里面.既比数组复杂又比树简单,所以链表经常被面试官 ...

  4. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  5. [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  6. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

  7. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  8. Leetcode解题-链表(2.2.0)基础类

    1 基类的作用 在开始练习LeetCode链表部分的习题之前,首先创建好一个Solution基类,其作用就是: Ø  规定好每个子Solution都要实现纯虚函数test做测试: Ø  提供了List ...

  9. LeetCode之“链表”:Intersection of Two Linked Lists

    此题扩展:链表有环,如何判断相交? 参考资料: 编程判断两个链表是否相交 面试精选:链表问题集锦 题目链接 题目要求: Write a program to find the node at whic ...

  10. LeetCode 单链表专题 (一)

    目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...

随机推荐

  1. orm功能封装

    封装功能: 查 : select **kwargs.keys() --返回-> obj -转为-->list [{},{}] ---> [obj,obj] class Models( ...

  2. 如何高效的阅读uni-app框架?(建议收藏)

    作者 | Jeskson来源 | 达达前端小酒馆 uni-app的框架,配置:page.json,manifest.json,package.json,vue.config.js.脚本,应用程序,ma ...

  3. 重装了服务器,用的是centos/php微信小程序版,centos 命令大全

    centos 命令大全 1.关机 (系统的关机.重启以及登出 ) 的命令 shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdo ...

  4. SpringCloud-Feign声明式服务调用

    在前面的文章中可以发现当我们通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率 ...

  5. 多catch块的代码优化

    一.多catch块的代码优化 在写代码时,多行存在不同的异常,使用try catch的话,习惯性的是有多个catch,如下所示: 注意到warning,文字描述如下: Reports identica ...

  6. IntelliJ IDEA 2019从入门到癫狂 图文教程!

    阅读本文大概需要 6 分钟. 作者:yizhiwazi 来源:www.jianshu.com/p/9c65b7613c30 前言:IntelliJ IDEA 如果说IntelliJ IDEA是一款现代 ...

  7. Shell命令行提示定制

    /******************************************************************************* * Shell命令行提示定制 * 说明 ...

  8. MYSQL报错:1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column

    1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'rpa ...

  9. 阿里云mysql数据库恢复到本地

    本地环境为win10,mysql引擎为InnoDB 第一步:服务里面停掉mysql 第二步:把my.ini 的 innodb_force_recovery  设置为0 第三步:把.frm和.idb文件 ...

  10. ubuntu 本地生成被浏览器信任的证书

    vhosts添加https证书两步: 1:生成证书: sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl ...