为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode876. 链表的中间结点 | Middle of the Linked List

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10600796.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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

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

Example 1:

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:

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

给定一个带有头结点 head 的非空单链表,返回链表的中间结点。

如果有两个中间结点,则返回第二个中间结点。 

示例 1:

输入:[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:

输入:[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 }

 

posted @ 2019-03-26 16:06  为敢技术  阅读(321)  评论(0编辑  收藏  举报