Reverse Nodes in k-Group

本文介绍了一种解决LeetCode上链表K个一组翻转问题的方法,包括迭代和递归两种实现方式,并提供了一种仅16行的非递归简洁解法。

【LeetCode】

题目描述:

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.

You may not alter the values in the nodes, only nodes itself may be changed.

Only constant memory is allowed.

For 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

我的解法,迭代,和反转单链表有相似之处,只不过要注意反转 k 个结点之后要注意这 k 个结点的头尾和谁相连。

 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 || head.next == null || k < 2)
12             return head;
13         ListNode dummy = new ListNode(0);
14         dummy.next = head;
15         ListNode begin = dummy;
16         int i = 0;
17         while(head != null){
18             i++;
19             if(i % k == 0){
20                 begin = reverse(begin, head.next);
21                 head = begin.next;
22             }else
23                 head = head.next;
24         }
25         return dummy.next;
26     }
27     public ListNode reverse(ListNode begin, ListNode end){ // 反转(begin, end)开区间之间的 k 个结点
28         ListNode prev = begin;
29         ListNode curr = begin.next;
30         ListNode first = curr;
31         while(curr != end){
32             ListNode next = curr.next;
33             curr.next = prev;
34             prev = curr;
35             curr = next;
36         }
37         begin.next = prev;
38         first.next = curr;
39         return first;
40     }
41 }

讨论区别人的递归版解法:还带注释的:

 1 public ListNode reverseKGroup(ListNode head, int k) {
 2     ListNode curr = head;
 3     int count = 0;
 4     while (curr != null && count != k) { // find the k+1 node
 5         curr = curr.next;
 6         count++;
 7     }
 8     if (count == k) { // if k+1 node is found
 9         curr = reverseKGroup(curr, k); // reverse list with k+1 node as head
10         // head - head-pointer to direct part, 
11         // curr - head-pointer to reversed part;
12         while (count-- > 0) { // reverse current k-group: 
13             ListNode tmp = head.next; // tmp - next head in direct part
14             head.next = curr; // preappending "direct" head to the reversed list 
15             curr = head; // move head of reversed part to a new node
16             head = tmp; // move "direct" head to the next node in direct part
17         }
18         head = curr;
19     }
20     return head;
21 }

 讨论区里别人的非递归版解法,16行,给跪了。。。

 1 public ListNode reverseKGroup(ListNode head, int k) {
 2     int n = 0;
 3     for (ListNode i = head; i != null; n++, i = i.next);
 4     
 5     ListNode dmy = new ListNode(0);
 6     dmy.next = head;
 7     for(ListNode prev = dmy, tail = head; n >= k; n -= k) {
 8         for (int i = 1; i < k; i++) {
 9             ListNode next = tail.next.next;
10             tail.next.next = prev.next;
11             prev.next = tail.next;
12             tail.next = next;
13         }
14         prev = tail;
15         tail = tail.next;
16     }
17     return dmy.next;
18 }

 

转载于:https://www.cnblogs.com/niuxichuan/p/7455553.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值