[Swift]LeetCode876. 鏈表的中間結點 | Middle of the Linked List

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-kusfiakh-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a non-empty, singly linked list with head node head, return a middle node of linked list.node

If there are two middle nodes, return the second middle node. git

Example 1:github

Input: [1,2,3,4,5]
Output: Node 3 from this list (Serialization: [3,4,5]) The returned node has value 3. (The judge's serialization of this node is [3,4,5]). Note that we returned a ListNode object ans, such that: ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL. 

Example 2:微信

Input: [1,2,3,4,5,6]
Output: Node 4 from this list (Serialization: [4,5,6]) Since the list has two middle nodes with values 3 and 4, we return the second one. 

Note:app

  • The number of nodes in the given list will be between 1 and 100.

給定一個帶有頭結點 head 的非空單鏈表,返回鏈表的中間結點。this

若是有兩個中間結點,則返回第二個中間結點。 spa

示例 1:code

輸入:[1,2,3,4,5]
輸出:此列表中的結點 3 (序列化形式:[3,4,5])
返回的結點值爲 3 。 (測評系統對該結點序列化表述是 [3,4,5])。
注意,咱們返回了一個 ListNode 類型的對象 ans,這樣:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.

示例 2:htm

輸入:[1,2,3,4,5,6]
輸出:此列表中的結點 4 (序列化形式:[4,5,6])
因爲該列表有兩個中間結點,值分別爲 3 和 4,咱們返回第二個結點。 

提示:

  • 給定鏈表的結點數介於 1 和 100 之間。

Runtime: 4 ms
Memory Usage: 19.1 MB
 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 class Solution {
13     func middleNode(_ head: ListNode?) -> ListNode? {
14         guard let _ = head else {
15             return nil
16         }
17         var fastNode = head
18         var slowNode = head
19         while let curNode = fastNode, let tmpNode = curNode.next {
20             slowNode = slowNode?.next
21             fastNode = tmpNode.next
22         }
23         return slowNode
24     }
25 }

4ms

 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 class Solution {
13     func middleNode(_ head: ListNode?) -> ListNode? {
14         var list = [ListNode]()
15         
16         var currentNode: ListNode? = head
17         while let node = currentNode {
18             list.append(node)
19             currentNode = node.next
20         }
21         
22         return list[list.count / 2]
23     }
24 }

4ms

 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 class Solution {
13     func middleNode(_ head: ListNode?) -> ListNode? {
14         var slow:ListNode? = head
15         var fast:ListNode? = head
16         while (fast != nil && fast?.next != nil)
17         {
18             slow = slow?.next
19             fast = fast?.next?.next
20         }
21         return slow
22     }
23 }

8ms

 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 class Solution {
13     func middleNode(_ head: ListNode?) -> ListNode? {
14       
15     var i = head!
16     var j = head!
17         while (j != nil && j.next != nil){
18              i = i.next!
19              if j.next != nil || j.next!.next != nil{
20              j = j.next!.next ?? ListNode(0)
21             }
22         }
23         return i
24     }
25 }        

24ms

 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 class Solution {
13     func middleNode(_ head: ListNode?) -> ListNode?
14     {
15         var x = head
16         var arr:[ListNode] = []
17         while x != nil {
18             arr.append(x!)
19             x = x?.next
20         }        
21         var index = arr.count/2        
22         return arr[index]
23     }
24 }
相關文章
相關標籤/搜索