- 
 
- 
  Notifications
 You must be signed in to change notification settings 
- Fork 9.2k
Add Solution 025[Java] #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
 
  Merged
 
 
 
 
  Merged
 Changes from all commits
 Commits
 
 
 File filter
Filter by extension
Conversations
 Failed to load comments. 
 
 
 
  Loading
 
 Jump to
 
 Jump to file
 
 
 
 Failed to load files. 
 
 
 
  Loading
 
 Diff view
Diff view
There are no files selected for viewing
 
 
 
 76 changes: 76 additions & 0 deletions
 
 
 
 solution/025.Reverse Nodes in k-Group/README.md
 
 
 
 
  
 
 
 
 
 
 
 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
 Learn more about bidirectional Unicode characters
 
 
 
 
 | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| ## k个一组翻转链表 | ||
| ### 题目描述 | ||
|  | ||
| 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表。 | ||
|  | ||
| k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序。 | ||
|  | ||
| 示例 : | ||
|  | ||
| 给定这个链表:1->2->3->4->5 | ||
|  | ||
| 当 k = 2 时,应当返回: 2->1->4->3->5 | ||
|  | ||
| 当 k = 3 时,应当返回: 3->2->1->4->5 | ||
|  | ||
| 说明 : | ||
|  | ||
| 你的算法只能使用常数的额外空间。 | ||
| 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换 | ||
|  | ||
| ### 解法 | ||
| 1. 在 head 节点前增加一个头节点 reNode 使所有的翻转操作情况一致。 | ||
| 2. 维护一个 num 计数,指针 pNode 从 head 节点开始,每经过 k 个节点,进行一次 k 个节点的翻转 | ||
| 3. 将翻转后的 k 个节点与前后组的节点相连 | ||
|  | ||
| ```java | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * public class ListNode { | ||
| * int val; | ||
| * ListNode next; | ||
| * ListNode(int x) { val = x; } | ||
| * } | ||
| */ | ||
| class Solution { | ||
| public ListNode reverseKGroup(ListNode head, int k) { | ||
| if(head == null || k < 2) { | ||
| return head; | ||
| } | ||
| int num = 0; | ||
| ListNode pNode = head; | ||
| ListNode lastNode = new ListNode(0); | ||
| ListNode reNode = lastNode; | ||
| lastNode.next = head; | ||
| while (pNode != null) { | ||
| num++; | ||
| if(num >= k) { | ||
| num = 0; | ||
| ListNode tempNode = pNode.next; | ||
| reserver(lastNode.next, k); | ||
| // k 个节点的尾节点指向下一组的头节点 | ||
| lastNode.next.next = tempNode; | ||
| // 上一组的尾节点指向当前 k 个节点的头节点 | ||
| tempNode = lastNode.next; | ||
| lastNode.next = pNode; | ||
|  | ||
| lastNode = tempNode; | ||
| pNode = lastNode.next; | ||
| } | ||
| else { | ||
| pNode = pNode.next; | ||
| } | ||
| } | ||
| return reNode.next; | ||
| } | ||
|  | ||
| private ListNode reserver(ListNode node, int i) { | ||
| if(i <= 1 || node.next == null) { | ||
| return node; | ||
| } | ||
| ListNode lastNode = reserver(node.next, i - 1); | ||
| lastNode.next = node; | ||
| return node; | ||
| } | ||
| } | ||
| ``` | 
 
 
 
 49 changes: 49 additions & 0 deletions
 
 
 
 solution/025.Reverse Nodes in k-Group/Solution.java
 
 
 
 
  
 
 
 
 
 
 
 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
 Learn more about bidirectional Unicode characters
 
 
 
 
 | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * public class ListNode { | ||
| * int val; | ||
| * ListNode next; | ||
| * ListNode(int x) { val = x; } | ||
| * } | ||
| */ | ||
| class Solution { | ||
| public ListNode reverseKGroup(ListNode head, int k) { | ||
| if(head == null || k < 2) { | ||
| return head; | ||
| } | ||
| int num = 0; | ||
| ListNode pNode = head; | ||
| ListNode lastNode = new ListNode(0); | ||
| ListNode reNode = lastNode; | ||
| lastNode.next = head; | ||
| while (pNode != null) { | ||
| num++; | ||
| if(num >= k) { | ||
| num = 0; | ||
| ListNode tempNode = pNode.next; | ||
| reserver(lastNode.next, k); | ||
| // k 个节点的尾节点指向下一组的头节点 | ||
| lastNode.next.next = tempNode; | ||
| // 上一组的尾节点指向当前 k 个节点的头节点 | ||
| tempNode = lastNode.next; | ||
| lastNode.next = pNode; | ||
|  | ||
| lastNode = tempNode; | ||
| pNode = lastNode.next; | ||
| } | ||
| else { | ||
| pNode = pNode.next; | ||
| } | ||
| } | ||
| return reNode.next; | ||
| } | ||
|  | ||
| private ListNode reserver(ListNode node, int i) { | ||
| if(i <= 1 || node.next == null) { | ||
| return node; | ||
| } | ||
| ListNode lastNode = reserver(node.next, i - 1); | ||
| lastNode.next = node; | ||
| return node; | ||
| } | ||
| } | 
 Add this suggestion to a batch that can be applied as a single commit.
 This suggestion is invalid because no changes were made to the code.
 Suggestions cannot be applied while the pull request is closed.
 Suggestions cannot be applied while viewing a subset of changes.
 Only one suggestion per line can be applied in a batch.
 Add this suggestion to a batch that can be applied as a single commit.
 Applying suggestions on deleted lines is not supported.
 You must change the existing code in this line in order to create a valid suggestion.
 Outdated suggestions cannot be applied.
 This suggestion has been applied or marked resolved.
 Suggestions cannot be applied from pending reviews.
 Suggestions cannot be applied on multi-line comments.
 Suggestions cannot be applied while the pull request is queued to merge.
 Suggestion cannot be applied right now. Please check back later.