mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #573 from GuoDuanLZ/master
添加 24两两交换链表中的节点,707设计链表 ,206反转链表,19删除链表的倒数第N个节点 Kotlin实现
This commit is contained in:
@ -184,6 +184,25 @@ var removeNthFromEnd = function(head, n) {
|
|||||||
return ret.next;
|
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)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
@ -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)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
@ -319,7 +319,20 @@ def reverse(pre, cur)
|
|||||||
reverse(cur, tem) # 通过递归实现双指针法中的更新操作
|
reverse(cur, tem) # 通过递归实现双指针法中的更新操作
|
||||||
end
|
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)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
@ -880,7 +880,73 @@ MyLinkedList.prototype.deleteAtIndex = function(index) {
|
|||||||
* obj.deleteAtIndex(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 > 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
|
||||||
|
cur?.next = ListNode(`val`)
|
||||||
|
cur?.next?.next = temp
|
||||||
|
this.next = pre.next
|
||||||
|
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--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user