From 07e95e7a97f1bdf55386cbc6c05f6b9cfba88a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Mon, 16 Aug 2021 13:00:02 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20203.=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E5=85=83=E7=B4=A0=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0203.移除链表元素.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index 9235d47e..f3724fc2 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -304,8 +304,7 @@ var removeElements = function(head, val) { }; ``` -Swift: - +Swift: ```swift /** * Definition for singly-linked list. From c7c52f7db6d92800a273057c3a5abc4cffcc8aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Mon, 16 Aug 2021 20:28:38 +0800 Subject: [PATCH 2/3] =?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=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0707.设计链表.md | 64 +++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 0aa038e8..33b02e56 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -948,6 +948,70 @@ class MyLinkedList { } ``` +Swift +```Swift +class MyLinkedList { + var size = 0 + let head: ListNode + + /** Initialize your data structure here. */ + init() { + head = ListNode(-1) + } + + /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */ + func get(_ index: Int) -> Int { + if size > 0 && index < size { + var tempHead = head + for _ in 0.. size { + return + } + let idx = (index >= 0 ? index : 0) + var tempHead = head + for _ in 0 ..< idx { + tempHead = tempHead.next! + } + let currentNode = tempHead.next + let newNode = ListNode(val, currentNode) + tempHead.next = newNode + size += 1 + } + + /** Delete the index-th node in the linked list, if the index is valid. */ + func deleteAtIndex(_ index: Int) { + if size > 0 && index < size { + var tempHead = head + for _ in 0 ..< index { + tempHead = tempHead.next! + } + tempHead.next = tempHead.next!.next + size -= 1 + } + } +} +``` ----------------------- From eaa0504acbdc195c0804ecaebd033528786e7939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Wed, 18 Aug 2021 19:47:30 +0800 Subject: [PATCH 3/3] =?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=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0206.翻转链表.md | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 8bb359bd..0c850d52 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -334,6 +334,43 @@ fun reverseList(head: ListNode?): ListNode? { } ``` +Swift: +```swift +/// 双指针法 (迭代) +/// - Parameter head: 头结点 +/// - Returns: 翻转后的链表头结点 +func reverseList(_ head: ListNode?) -> ListNode? { + if head == nil || head?.next == nil { + return head + } + var pre: ListNode? = nil + var cur: ListNode? = head + var temp: ListNode? = nil + while cur != nil { + temp = cur?.next + cur?.next = pre + pre = cur + cur = temp + } + return pre +} + +/// 递归 +/// - Parameter head: 头结点 +/// - Returns: 翻转后的链表头结点 +func reverseList2(_ head: ListNode?) -> ListNode? { + return reverse(pre: nil, cur: head) +} +func reverse(pre: ListNode?, cur: ListNode?) -> ListNode? { + if cur == nil { + return pre + } + let temp: ListNode? = cur?.next + cur?.next = pre + return reverse(pre: cur, cur: temp) +} +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321)