[Swift]LeetCode61. 旋轉鏈表 | Rotate List

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

Given a linked list, rotate the list to the right by k places, where k is non-negative.git

Example 1:github

Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL
Explanation:
rotate 1 steps to the right: 5->1->2->3->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL

Example 2:微信

Input: 0->1->2->NULL, k = 4
Output: 
Explanation:
rotate 1 steps to the right: 2->0->1->NULL
rotate 2 steps to the right: 1->2->0->NULL
rotate 3 steps to the right: 
rotate 4 steps to the right: 2->0->1->NULL0->1->2->NULL2->0->1->NULL

給定一個鏈表,旋轉鏈表,將鏈表每一個節點向右移動 個位置,其中 是非負數。

示例 1:app

輸入: 1->2->3->4->5->NULL, k = 2
輸出: 4->5->1->2->3->NULL
解釋:
向右旋轉 1 步: 5->1->2->3->4->NULL
向右旋轉 2 步: 4->5->1->2->3->NULL

示例 2:ide

輸入: 0->1->2->NULL, k = 4
輸出: 
解釋:
向右旋轉 1 步: 2->0->1->NULL
向右旋轉 2 步: 1->2->0->NULL
向右旋轉 3 步: 
向右旋轉 4 步: 2->0->1->NULL0->1->2->NULL2->0->1->NULL

20ms
 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 rotateRight(_ head: ListNode?, _ k: Int) -> ListNode? {
14         // Count length
15         var l = 0
16         var p = head
17         var last = p
18         while p != nil {
19             l += 1
20             last = p
21             p = p!.next
22         }
23         
24         if l == 0 { return head }
25         
26         // Rotate
27         let dummyHead = ListNode(0)
28         dummyHead.next = head
29         p = dummyHead
30         for _ in 0..<(l - k % l) % l {
31             p = p!.next
32         }
33         let ret = p!.next
34         p!.next = nil
35         last!.next = dummyHead.next
36         return ret
37     }
38 }

24msspa

 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 rotateRight(_ head: ListNode?, _ k: Int) -> ListNode? {
14         if(head?.next == nil || k == 0){
15             return head
16         }
17         var current = head
18         var array = [Int]()
19         var n = 0
20         while(current != nil){
21             array.append(current!.val)
22             n += 1
23             current = current!.next  
24         }
25         var preHead = ListNode(0)
26         current = preHead
27         var start = (n - (k % n)) % n
28         for i in start ..< (start + n){
29             var newNode = ListNode(array[(i % n)])
30             current!.next = newNode
31             current = current!.next
32         }
33         return preHead.next  
34     }
35 }

28ms指針

 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 rotateRight(_ head: ListNode?, _ k: Int) -> ListNode? {
14         if head == nil || head?.next == nil || k == 0 {
15             return head
16         }
17         var (fastPtr, slowPtr, length) = (head, head, 1)
18         while let _ = fastPtr?.next {   // get list length
19             length += 1
20             fastPtr = fastPtr?.next
21         }
22         
23         let slowLenth = length - k%length
24         for _ in 1..<slowLenth {       // how many steps slowPtr needs to go
25             slowPtr = slowPtr?.next
26         }
27         
28         fastPtr?.next = head            // perform rotation
29         let newHead = slowPtr?.next
30         slowPtr?.next = nil
31         
32         return newHead;
33     }
34 }

28mscode

 1 class Solution {
 2     func rotateRight(_ head: ListNode?, _ k: Int) -> ListNode? {
 3         guard let head = head else {
 4             return nil
 5         }
 6         var newHeadNode: ListNode! = head;
 7         var newTailNode: ListNode! = head;
 8         var tailNode: ListNode! = head;
 9         var linkLength = 1;
10         while tailNode.next != nil {
11             tailNode = tailNode.next;
12             linkLength += 1
13         }
14         if linkLength <= 1 {
15             return head;
16         }
17         let k = k % linkLength
18         for _ in 0 ..< abs(linkLength - k - 1)  {
19             if newTailNode.next != nil {
20                 newTailNode = newTailNode.next
21             } else {
22                 newTailNode = head
23             }
24         }
25         if newTailNode.next != nil {
26             newHeadNode = newTailNode.next
27             tailNode.next = head;
28             newTailNode.next = nil;
29             return newHeadNode;
30         } else {
31             return head;
32         }
33     }
34 }

40msorm

 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 rotateRight(_ head: ListNode?, _ k: Int) -> ListNode? {
14         /*
15          *  思路,首先咱們計算出鏈表的長度,這樣據k值能夠計算出鏈表實際移動距離
16          *  而後,經過兩個指針來記錄移動位置
17          */
18         var length = 0
19         var temp = head
20         while temp?.next != nil {
21             length += 1
22             temp = temp?.next
23         }
24         
25         // 長度已經獲取length+1
26         let stride = k % (length + 1)
27         if stride == 0{
28             return head
29         }
30         // stride爲實際須要移動的距離
31         // 定義兩個指針,一個指向鏈表倒數第stride+1個,一個指向最後一個
32         var start: ListNode?
33         var end: ListNode?
34         var tem: ListNode?
35         for index in 0...length{
36             if index == 0 {
37                 tem = head
38                 end = head
39             }else{
40                 tem = tem?.next
41                 end = end?.next
42             }
43             if index == length + 1 - (stride + 1) {
44                 start = tem
45             }
46         }
47         
48         /*
49          *  修改節點
50          */
51         let res = start?.next
52         start?.next = nil
53         end?.next = head
54         
55         return res
56     }
57 }

48ms

 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 rotateRight(_ head: ListNode?, _ k: Int) -> ListNode? {
14         guard let head = head else {
15             return nil
16         }
17 
18         guard k > 0 else {
19             return head
20         }
21 
22         var length = 0
23         var next: ListNode? = head
24         var tail = head
25         while next != nil {
26             tail = next!
27             next = next?.next
28             length += 1
29         }
30         
31         if (length == 1) {
32             return head
33         }
34 
35         let k = k > length ? k % length : k
36         if (k == length || k == 0) {
37             return head
38         }
39         
40         next = head
41         for _ in 0..<length - k - 1 {
42             next = next?.next
43         }
44         let start = next?.next
45         tail.next = head
46         next?.next = nil
47         return start
48     }
49 }
相關文章
相關標籤/搜索