25. k个一组翻转链表-LeetCode

心得:反转链表加强版,加头节点简化操作,然后写一个方法调用

反转链表,注意next的操作,边界条件!!

代码:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 class Solution {
10      public ListNode reverseKGroup(ListNode head, int k) {
11           if(head==null)
12               return null;
13             ListNode rhead=new ListNode(-1);
14             rhead.next=head;
15             ListNode tmp=run(rhead,k);
16             while(tmp!=null)
17             {
18                 tmp=run(tmp,k);
19             }
20             return rhead.next;
21         }
22       public ListNode run(ListNode head,int k)
23       {
24           ListNode start=head.next;
25           ListNode next=null;
26           ListNode end=null;
27           ListNode ans=null;
28           int sum=k;
29           while(start!=null&&sum>0)
30           {
31               start=start.next;
32              sum--;
33           }
34           if(sum!=0)
35               return null;
36           start=head.next;
37         //  next=null;
38           if(start.next!=null)
39               next=start.next;
40           if(start.next!=null&&start.next.next!=null)
41               end=start.next.next;
42         while(next!=null&&k>1)
43         {
44             next.next=start;
45             start=next;
46             next=end;
47             if(end!=null)
48              end=end.next;
49             k--;
50         }
51         ans=head.next;
52           head.next.next=next;
53           head.next=start;
54           return ans;
55       }
56 }

原文地址:https://www.cnblogs.com/pc-m/p/10888941.html

时间: 2024-08-30 12:39:05

25. k个一组翻转链表-LeetCode的相关文章

[leetcode] 25. k个一组翻转链表

25. k个一组翻转链表 仍然是链表处理问题,略微复杂一点,边界条件得想清楚,画画图就会比较明确了. reverse函数表示从 front.next节点开始,一共k个节点做反转. 即: 1>2>3>4>5 ,k = 2.当front为1时, 执行reverse后: 1>3>2>4>5 同上个题一样,申请一个空的(无用的)头指针指向第一个节点,会方便许多. public ListNode reverseKGroup(ListNode head, int k)

[LeetCode] 25. K 个一组翻转链表 ☆☆☆☆☆(链表)

https://leetcode-cn.com/problems/reverse-nodes-in-k-group/solution/javadi-gui-fang-fa-100-by-chadriy-imdgvs6udp/ https://leetcode-cn.com/problems/reverse-nodes-in-k-group/solution/tu-jie-kge-yi-zu-fan-zhuan-lian-biao-by-user7208t/ 描述 给你一个链表,每 k 个节点一组

25. K 个一组翻转链表

给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度. 如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序. 示例 : 给定这个链表:1->2->3->4->5 当 k = 2 时,应当返回: 2->1->4->3->5 当 k = 3 时,应当返回: 3->2->1->4->5 来源:力扣(LeetCode)链接:https://leetcode-cn.com

25. k个一组翻转链表

题目描述: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. 示例 : 给定这个链表:1->2->3->4->5 当 k = 2 时,应当返回: 2->1->4->3->5 当 k = 3 时,应当返回: 3->2->1->4->5 说明 : 你的算法只能使用常数的额外空间. 你不能只是单纯的改变节点内部的值,

(Java) LeetCode 25. Reverse Nodes in k-Group —— k个一组翻转链表

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

[LintCode] Reverse Nodes in k-Group 每k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

[LeetCode]25. Reverse Nodes in k-Group k个一组翻转链表

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

k个一组翻转链表(java实现)

题目: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. 示例 : 给定这个链表:1->2->3->4->5 当 k = 2 时,应当返回: 2->1->4->3->5 当 k = 3 时,应当返回: 3->2->1->4->5 说明 : 你的算法只能使用常数的额外空间. 你不能只是单纯的改变节点内部的值,而是

[LeetCode] 25. Reverse Nodes in k-Group 每k个一组翻转链表

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