• Reverse Nodes in k-Group


    【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 }
  • 相关阅读:
    软件工程实践总结
    beta答辩总结
    beta冲刺(6/7)
    beta 冲刺(5/7)
    beta冲刺(4/7)
    beta冲刺(3/7)
    beta冲刺(2/7)
    beta冲刺(1/7)
    CentOS7安装新版git
    NameError: name 'reload' is not defined
  • 原文地址:https://www.cnblogs.com/niuxichuan/p/7455553.html
Copyright © 2020-2023  润新知