[关闭]
@cxm-2016 2016-12-23T21:45:37.000000Z 字数 2355 阅读 2512

算法:两个单链表生成相加链表

算法

版本:2
作者:陈小默
声明:禁止商业,禁止转载

发布于作业部落SCDN


题目:假设链表中的每一节点的值都在0~9之间,于是我们可以使用整个链表作为一个整数。
例如:

链表9->3->7和链表6->3相加后得到链表1->0->0->0

解法:我们可以先将两个链表转置,然后从低位到高位依次运算。

一下代码给出了三种实现方式,后一种是前一种的简化

  1. data class Node(var value: Int, var next: Node?) {
  2. override fun toString(): String {
  3. val builder = StringBuilder().append('[')
  4. var node: Node? = this.next
  5. builder.append(value)
  6. while (node != null) {
  7. builder.append(',').append('\t').append(node.value)
  8. node = node.next
  9. }
  10. return builder.append(']').toString()
  11. }
  12. }
  13. fun reverseList(list: Node): Node {
  14. var pre: Node? = null
  15. var next: Node? = null
  16. var head: Node? = list
  17. while (head != null) {
  18. next = head.next
  19. head.next = pre
  20. pre = head
  21. head = next
  22. }
  23. return pre!!
  24. }
  25. fun addList_v1(list1: Node, list2: Node): Node {
  26. var l1: Node? = reverseList(list1)
  27. var l2: Node? = reverseList(list2)
  28. var next: Node? = null
  29. var node: Node?
  30. var head: Node? = null
  31. var carry = false
  32. while (l1 != null && l2 != null) {
  33. var value = l1.value + l2.value
  34. if (carry) value++
  35. carry = value > 9
  36. if (carry) value %= 10
  37. node = Node(value, null)
  38. if (head == null) {
  39. head = node
  40. next = node
  41. } else {
  42. next!!.next = node
  43. next = next.next
  44. }
  45. l1 = l1.next
  46. l2 = l2.next
  47. }
  48. while (l1 != null) {
  49. var value = l1.value
  50. if (carry) value++
  51. carry = value > 9
  52. if (carry) value %= 10
  53. node = Node(value, null)
  54. if (head == null) {
  55. head = node
  56. next = node
  57. } else {
  58. next!!.next = node
  59. next = next.next
  60. }
  61. l1 = l1.next
  62. }
  63. while (l2 != null) {
  64. var value = l2.value
  65. if (carry) value++
  66. carry = value > 9
  67. if (carry) value %= 10
  68. node = Node(value, null)
  69. if (head == null) {
  70. head = node
  71. next = node
  72. } else {
  73. next!!.next = node
  74. next = next.next
  75. }
  76. l2 = l2.next
  77. }
  78. if (carry) {
  79. node = Node(1, null)
  80. next!!.next = node
  81. }
  82. return reverseList(head!!)
  83. }
  84. fun addList_v2(list1: Node, list2: Node): Node {
  85. var l1: Node? = reverseList(list1)
  86. var l2: Node? = reverseList(list2)
  87. var next: Node?
  88. var head: Node? = null
  89. var carry = 0
  90. while (l1 != null && l2 != null) {
  91. val value = l1.value + l2.value + carry
  92. carry = value / 10
  93. next = head
  94. head = Node(value % 10, next)
  95. l1 = l1.next
  96. l2 = l2.next
  97. }
  98. while (l1 != null) {
  99. val value = l1.value + carry
  100. carry = value / 10
  101. next = head
  102. head = Node(value % 10, next)
  103. l1 = l1.next
  104. }
  105. while (l2 != null) {
  106. val value = l2.value + carry
  107. carry = value / 10
  108. next = head
  109. head = Node(value % 10, next)
  110. l2 = l2.next
  111. }
  112. if (carry == 1) {
  113. next = head
  114. head = Node(1, next)
  115. }
  116. return head!!
  117. }
  118. fun addList(list1: Node, list2: Node): Node {
  119. var l1: Node? = reverseList(list1)
  120. var l2: Node? = reverseList(list2)
  121. var n: Int
  122. var n1: Int
  123. var n2: Int
  124. var next: Node?
  125. var head: Node? = null
  126. var carry = 0
  127. while (l1 != null || l2 != null) {
  128. n1 = if (l1 != null) l1.value else 0
  129. n2 = if (l2 != null) l2.value else 0
  130. n = n1 + n2 + carry
  131. carry = n / 10
  132. next = head
  133. head = Node(n % 10, next)
  134. l1 = if (l1 != null) l1.next else null
  135. l2 = if (l2 != null) l2.next else null
  136. }
  137. if (carry == 1) {
  138. next = head
  139. head = Node(1, next)
  140. }
  141. return head!!
  142. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注