From 9ecc7e9be3d54469c2828a4093746857a4c1d157 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Thu, 18 Apr 2024 09:01:21 +0100 Subject: [PATCH 1/2] Swift implementation for LCCI 02.02 Signed-off-by: Lanre Adedara --- .../02.02.Kth Node From End of List/README.md | 34 +++++++++++++++++++ .../README_EN.md | 34 +++++++++++++++++++ .../Solution.swift | 31 +++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 lcci/02.02.Kth Node From End of List/Solution.swift diff --git a/lcci/02.02.Kth Node From End of List/README.md b/lcci/02.02.Kth Node From End of List/README.md index 28b6653c49462..785680474ed4a 100644 --- a/lcci/02.02.Kth Node From End of List/README.md +++ b/lcci/02.02.Kth Node From End of List/README.md @@ -203,6 +203,40 @@ var kthToLast = function (head, k) { }; ``` +```swift +/** + * Definition for singly-linked list. + * public class ListNode { + * var val: Int + * var next: ListNode? + * init(_ x: Int, _ next: ListNode? = nil) { + * self.val = x + * self.next = next + * } + * } + */ + +class Solution { + func kthToLast(_ head: ListNode?, _ k: Int) -> Int { + var slow = head + var fast = head + var k = k + + while k > 0 { + fast = fast?.next + k -= 1 + } + + while fast != nil { + slow = slow?.next + fast = fast?.next + } + + return slow?.val ?? 0 + } +} +``` + diff --git a/lcci/02.02.Kth Node From End of List/README_EN.md b/lcci/02.02.Kth Node From End of List/README_EN.md index d994e8cab5f21..c0c9085cafc0d 100644 --- a/lcci/02.02.Kth Node From End of List/README_EN.md +++ b/lcci/02.02.Kth Node From End of List/README_EN.md @@ -205,6 +205,40 @@ var kthToLast = function (head, k) { }; ``` +```swift +/** + * Definition for singly-linked list. + * public class ListNode { + * var val: Int + * var next: ListNode? + * init(_ x: Int, _ next: ListNode? = nil) { + * self.val = x + * self.next = next + * } + * } + */ + +class Solution { + func kthToLast(_ head: ListNode?, _ k: Int) -> Int { + var slow = head + var fast = head + var k = k + + while k > 0 { + fast = fast?.next + k -= 1 + } + + while fast != nil { + slow = slow?.next + fast = fast?.next + } + + return slow?.val ?? 0 + } +} +``` + diff --git a/lcci/02.02.Kth Node From End of List/Solution.swift b/lcci/02.02.Kth Node From End of List/Solution.swift new file mode 100644 index 0000000000000..d42d107223b2b --- /dev/null +++ b/lcci/02.02.Kth Node From End of List/Solution.swift @@ -0,0 +1,31 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * var val: Int + * var next: ListNode? + * init(_ x: Int, _ next: ListNode? = nil) { + * self.val = x + * self.next = next + * } + * } + */ + +class Solution { + func kthToLast(_ head: ListNode?, _ k: Int) -> Int { + var slow = head + var fast = head + var k = k + + while k > 0 { + fast = fast?.next + k -= 1 + } + + while fast != nil { + slow = slow?.next + fast = fast?.next + } + + return slow?.val ?? 0 + } +} \ No newline at end of file From 167f2b14bb48102518390db9f6bdcf0deea413c7 Mon Sep 17 00:00:00 2001 From: klever34 Date: Thu, 18 Apr 2024 08:49:53 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- lcci/02.02.Kth Node From End of List/README.md | 6 +++--- lcci/02.02.Kth Node From End of List/README_EN.md | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lcci/02.02.Kth Node From End of List/README.md b/lcci/02.02.Kth Node From End of List/README.md index 785680474ed4a..8b013264d05a0 100644 --- a/lcci/02.02.Kth Node From End of List/README.md +++ b/lcci/02.02.Kth Node From End of List/README.md @@ -221,17 +221,17 @@ class Solution { var slow = head var fast = head var k = k - + while k > 0 { fast = fast?.next k -= 1 } - + while fast != nil { slow = slow?.next fast = fast?.next } - + return slow?.val ?? 0 } } diff --git a/lcci/02.02.Kth Node From End of List/README_EN.md b/lcci/02.02.Kth Node From End of List/README_EN.md index c0c9085cafc0d..785b3fb5a598b 100644 --- a/lcci/02.02.Kth Node From End of List/README_EN.md +++ b/lcci/02.02.Kth Node From End of List/README_EN.md @@ -223,17 +223,17 @@ class Solution { var slow = head var fast = head var k = k - + while k > 0 { fast = fast?.next k -= 1 } - + while fast != nil { slow = slow?.next fast = fast?.next } - + return slow?.val ?? 0 } }