From cb16c9fe1823b6295228dcd47a19aef3ef7edfac Mon Sep 17 00:00:00 2001 From: wz-mibookwindows Date: Sat, 7 Aug 2021 08:28:16 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=2024=E4=B8=A4=E4=B8=A4?= =?UTF-8?q?=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9=20Kotlin=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0024.两两交换链表中的节点.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index b285b2d4..cae69cea 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -228,6 +228,27 @@ var swapPairs = function (head) { }; ``` +Kotlin: + +```kotlin +fun swapPairs(head: ListNode?): ListNode? { + val dummyNode = ListNode(0).apply { + this.next = head + } + var cur: ListNode? = dummyNode + while (cur?.next != null && cur.next?.next != null) { + val temp = cur.next + val temp2 = cur.next?.next?.next + cur.next = cur.next?.next + cur.next?.next = temp + cur.next?.next?.next = temp2 + cur = cur.next?.next + } + return dummyNode.next +} +``` + + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From cb65921f6cb9f6c983af9ff2f8c02f75f6f4d1d4 Mon Sep 17 00:00:00 2001 From: wz-mibookwindows Date: Sat, 7 Aug 2021 10:55:06 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20707=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=20Kotlin=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0707.设计链表.md | 65 +++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 349e2409..f7b03c8b 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -880,7 +880,72 @@ MyLinkedList.prototype.deleteAtIndex = function(index) { * obj.deleteAtIndex(index) */ ``` +Kotlin: +```kotlin +class MyLinkedList { + var next: ListNode? = null + + var size: Int = 0 + + fun get(index: Int): Int { + if (index + 1 > size) return -1 + var cur = this.next + for (i in 0 until index) { + cur = cur?.next + } + return cur?.`val` ?: -1 + } + + fun addAtHead(`val`: Int) { + val head = ListNode(`val`) + head.next = this.next + this.next = head + size++ + } + + fun addAtTail(`val`: Int) { + val pre = ListNode(0) + pre.next = this.next + var cur: ListNode? = pre + while (cur?.next != null) { + cur = cur.next + } + cur?.next = ListNode(`val`) + this.next = pre.next + size++ + } + + fun addAtIndex(index: Int, `val`: Int) { + if (index == 0) return addAtHead(`val`) + if (index == size) return addAtTail(`val`) + if (index > size) return + var cur = this.next + for (i in 0 until index - 1) { + cur = cur?.next + } + val temp = cur?.next + cur?.next = ListNode(`val`) + cur?.next?.next = temp + size++ + } + + fun deleteAtIndex(index: Int) { + if (index + 1 > size) return + val pre = ListNode(0) + pre.next = this.next + var cur: ListNode? = pre + for (i in 0 until index) { + cur = cur?.next + } + val temp = cur?.next?.next + cur?.next?.next = null + cur?.next = temp + this.next = pre.next + size-- + } +} +``` From 91c6ecdee95f5eb97b53590a81e242ef6d5f75c7 Mon Sep 17 00:00:00 2001 From: wz-mibookwindows Date: Sat, 7 Aug 2021 11:04:11 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=20707=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=20addAtIndex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0707.设计链表.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index f7b03c8b..5e748a24 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -917,16 +917,17 @@ class MyLinkedList { } fun addAtIndex(index: Int, `val`: Int) { - if (index == 0) return addAtHead(`val`) - if (index == size) return addAtTail(`val`) if (index > size) return - var cur = this.next - for (i in 0 until index - 1) { + val pre = ListNode(0) + pre.next = this.next + var cur:ListNode? = pre + for (i in 0 until index) { cur = cur?.next } val temp = cur?.next cur?.next = ListNode(`val`) cur?.next?.next = temp + this.next = pre.next size++ } From 9dd846ec55f687a701631ff1a311e340283793bb Mon Sep 17 00:00:00 2001 From: wz-mibookwindows Date: Sat, 7 Aug 2021 11:39:11 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20206=E5=8F=8D=E8=BD=AC?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=20Kotlin=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0206.翻转链表.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 4efdda3b..27a2e903 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -319,7 +319,20 @@ def reverse(pre, cur) reverse(cur, tem) # 通过递归实现双指针法中的更新操作 end ``` - +Kotlin: +```Kotlin +fun reverseList(head: ListNode?): ListNode? { + var pre: ListNode? = null + var cur = head + while (cur != null) { + val temp = cur.next + cur.next = pre + pre = cur + cur = temp + } + return pre +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From 0f4c80a935bdbc64bbcc594f5507ce5aa5da9e53 Mon Sep 17 00:00:00 2001 From: wz-mibookwindows Date: Sat, 7 Aug 2021 12:11:40 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=2019=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=ACN?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9=20Kotlin=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0019.删除链表的倒数第N个节点.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index 52735794..4b17c972 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -184,6 +184,25 @@ var removeNthFromEnd = function(head, n) { return ret.next; }; ``` +Kotlin: +```Kotlin +fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? { + val pre = ListNode(0).apply { + this.next = head + } + var fastNode: ListNode? = pre + var slowNode: ListNode? = pre + for (i in 0..n) { + fastNode = fastNode?.next + } + while (fastNode != null) { + slowNode = slowNode?.next + fastNode = fastNode.next + } + slowNode?.next = slowNode?.next?.next + return pre.next +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)