Skip to content

Commit

Permalink
Swift: 876. Middle of the Linked List
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr committed Aug 15, 2023
1 parent 7c19317 commit 556a772
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions swift/0876-middle-of-the-linked-list.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func middleNode(_ head: ListNode?) -> ListNode? {
var current = head
var middle = head
while let next = current?.next {
current = next.next
middle = middle?.next
}
return middle
}
}

0 comments on commit 556a772

Please sign in to comment.