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

[Swift]LeetCode25. k个一组翻转链表 | Reverse Nodes in k-Group

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

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

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

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

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

Example:

Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

Note:

  • Only constant extra memory is allowed.
  • You may not alter the values in the list's nodes, only nodes itself may be changed.

给出一个链表,每 个节点一组进行翻转,并返回翻转后的链表。

是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 的整数倍,那么将最后剩余节点保持原有顺序。

示例 :

给定这个链表:1->2->3->4->5

当 = 2 时,应当返回: 2->1->4->3->5

当 = 3 时,应当返回: 3->2->1->4->5

说明 :

  • 你的算法只能使用常数的额外空间。
  • 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

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 reverseKGroup(_ head: ListNode?, _ k: Int) -> ListNode? {
14         let dummyHead = ListNode(0)
15         dummyHead.next = head
16 
17         var previous: ListNode? = dummyHead
18         var first = head
19         var kthNode: ListNode? = first
20         for _ in 1..<k {
21             kthNode = kthNode?.next
22         }
23 
24         while kthNode != nil {
25 
26             let nextNode = kthNode?.next
27 
28             kthNode?.next = nil
29 
30             previous?.next = reverseList(head: first)
31             first?.next = nextNode
32 
33             previous = first
34             first = previous?.next
35             kthNode = first
36             for _ in 1..<k {
37                 kthNode = kthNode?.next
38             }
39 
40         }
41 
42         return dummyHead.next
43     }
44 
45     func reverseList(head: ListNode?) -> ListNode? {
46         var oldList = head
47         var newList: ListNode? = nil
48 
49         while let node = oldList {
50             let nextNode = node.next
51             node.next = newList
52             newList = node
53 
54             oldList = nextNode
55         }
56         return newList
57     }
58 }

52ms

 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 reverseKGroup(_ head: ListNode?, _ k: Int) -> ListNode? {
14         if head?.next == nil {
15             return head
16         }
17         let dummy: ListNode? = ListNode(0)
18         dummy!.next = head
19         var pre = dummy, start = dummy, end = dummy
20         while end != nil {
21             end = pre
22             for _ in 0..<k {
23                 end = end?.next
24                 if end == nil {
25                     return dummy?.next
26                 }
27             }
28             for _ in 1..<k {
29                 start = pre!.next
30                 pre!.next = start!.next
31                 start!.next = end!.next
32                 end!.next = start
33             }
34             for _ in 0..<k {
35                 pre = pre!.next
36             }
37         }
38         return dummy!.next
39     }
40 }

56ms

 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 reverseKGroup(_ head: ListNode?, _ k: Int) -> ListNode? {
14         let dummyHead = ListNode(0)
15         dummyHead.next = head
16         var previous = dummyHead
17         var start = dummyHead.next
18         var end = start
19         while end != nil {
20             for _ in 0..<k - 1 {
21                 end = end == nil ? nil : end!.next
22             }
23             if end != nil {
24                 let next = end!.next
25                 var newStart = start
26                 while start!.next !== next {
27                     let nextNext = start!.next!.next
28                     previous.next = start!.next
29                     start!.next!.next = newStart
30                     newStart = previous.next
31                     start!.next = nextNext
32                 }
33                 previous = start!
34                 start = next
35                 end = start
36             }
37         }
38         return dummyHead.next
39     }
40 }

60ms

 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 reverseKGroup(_ head: ListNode?, _ k: Int) -> ListNode? {
14         if k == 1 {
15             return head
16         }
17         
18         var nextNode = head
19         var prevTail: ListNode?
20         var res: ListNode?
21         while nextNode != nil {
22             var newHead: ListNode?
23             var newTail: ListNode?
24 
25             (newHead, nextNode, newTail) = reverseK(node: nextNode,parent: nil, k: k-1)
26             if res == nil {
27                 res = newHead
28             }
29             prevTail?.next = newHead
30             prevTail = newTail
31             print(newHead?.val, nextNode?.val)
32         }
33         return res
34         
35     }
36     
37     func reverseK(node: ListNode?, parent: ListNode?, k: Int) -> (ListNode?, ListNode?, ListNode?) {
38         if k <= 0 {
39             let temp = node?.next
40             node?.next = parent
41             return (node, temp, nil)
42         } else if node == nil {
43             return (nil, nil, nil)
44         }
45         
46         let (newHead, nextNode, _) = reverseK(node: node?.next, parent: node, k: k-1)
47         
48         if newHead != nil {
49             var tail: ListNode?
50             if parent == nil {
51                 node?.next = nextNode
52                 tail = node
53             } else {
54                 node?.next = parent
55             }
56             return (newHead, nextNode, tail)
57         } else {
58             if parent == nil {
59                 return (node, nil, nil)
60             } else {
61                 return (nil, nil, nil)
62             }
63         }
64     }
65 }

84ms

 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 reverseKGroup(_ head: ListNode?, _ k: Int) -> ListNode? {
14         var head:ListNode? = head
15         var current_node:ListNode?  = head
16         var count:Int = 0
17         while(current_node != nil && count != k)
18         {
19             current_node = current_node!.next
20             count++
21         }
22         if count == k
23         {
24             // 递归的解决子问题
25             current_node = reverseKGroup(current_node, k)
26             while (count-- > 0)
27             {
28                 var temp:ListNode? = head!.next
29                 head!.next = current_node
30                 current_node = head
31                 head = temp
32             }
33             //最终,该段的所有节点将会截空,head应指向current_node
34             head = current_node
35         }
36         return head
37     }
38 }
39 
40 /*扩展Int类,实现自增++、自减--运算符*/
41 extension Int{
42     //后缀++:先执行表达式后再自增
43     static postfix func ++(num:inout Int) -> Int {
44         //输入输出参数num
45         let temp = num
46         //num加1
47         num += 1
48         //返回加1前的数值
49         return temp
50     }
51     //后缀--:先执行表达式后再自减
52     static postfix func --(num:inout Int) -> Int {
53         //输入输出参数num
54         let temp = num
55         //num减1
56         num -= 1
57          //返回减1前的数值
58         return temp
59     }
60 }

 

posted @ 2018-11-01 20:43  为敢技术  阅读(457)  评论(0编辑  收藏  举报