Skip to content

Commit 026a669

Browse files
authored
feat: add swift solution to lc problem: No.0160 (doocs#721)
No.0160.Intersection of Two Linked Lists
1 parent 816f7e5 commit 026a669

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

solution/0100-0199/0160.Intersection of Two Linked Lists/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,47 @@ function getIntersectionNode(
228228
}
229229
```
230230

231+
### **Swift**
232+
233+
```swift
234+
/**
235+
* Definition for singly-linked list.
236+
* public class ListNode {
237+
* public var val: Int
238+
* public var next: ListNode?
239+
* public init(_ val: Int) {
240+
* self.val = val
241+
* self.next = nil
242+
* }
243+
* }
244+
*/
245+
246+
class Solution {
247+
func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
248+
249+
guard let _ = headA, let _ = headB else {
250+
return nil
251+
}
252+
253+
var nodeA = headA
254+
var nodeB = headB
255+
256+
while nodeA != nodeB {
257+
nodeA = nodeA != nil ? nodeA?.next : headB
258+
nodeB = nodeB != nil ? nodeB?.next : headA
259+
}
260+
261+
return nodeA
262+
}
263+
}
264+
265+
extension ListNode: Equatable {
266+
public static func ==(lhs: ListNode, rhs: ListNode) -> Bool {
267+
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
268+
}
269+
}
270+
```
271+
231272
### **...**
232273

233274
```

solution/0100-0199/0160.Intersection of Two Linked Lists/README_EN.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,47 @@ function getIntersectionNode(
214214
}
215215
```
216216

217+
### **Swift**
218+
219+
```swift
220+
/**
221+
* Definition for singly-linked list.
222+
* public class ListNode {
223+
* public var val: Int
224+
* public var next: ListNode?
225+
* public init(_ val: Int) {
226+
* self.val = val
227+
* self.next = nil
228+
* }
229+
* }
230+
*/
231+
232+
class Solution {
233+
func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
234+
235+
guard let _ = headA, let _ = headB else {
236+
return nil
237+
}
238+
239+
var nodeA = headA
240+
var nodeB = headB
241+
242+
while nodeA != nodeB {
243+
nodeA = nodeA != nil ? nodeA?.next : headB
244+
nodeB = nodeB != nil ? nodeB?.next : headA
245+
}
246+
247+
return nodeA
248+
}
249+
}
250+
251+
extension ListNode: Equatable {
252+
public static func ==(lhs: ListNode, rhs: ListNode) -> Bool {
253+
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
254+
}
255+
}
256+
```
257+
217258
### **...**
218259

219260
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public var val: Int
5+
* public var next: ListNode?
6+
* public init(_ val: Int) {
7+
* self.val = val
8+
* self.next = nil
9+
* }
10+
* }
11+
*/
12+
13+
class Solution {
14+
func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
15+
guard let _ = headA, let _ = headB else {
16+
return nil
17+
}
18+
19+
var nodeA = headA
20+
var nodeB = headB
21+
22+
while nodeA != nodeB {
23+
nodeA = nodeA != nil ? nodeA?.next : headB
24+
nodeB = nodeB != nil ? nodeB?.next : headA
25+
}
26+
27+
return nodeA
28+
}
29+
}
30+
31+
extension ListNode: Equatable {
32+
public static func == (lhs: ListNode, rhs: ListNode) -> Bool {
33+
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
34+
}
35+
}

0 commit comments

Comments
 (0)