zoukankan      html  css  js  c++  java
  • [leetcode]Reverse Nodes in k-Group

    Reverse Nodes in k-Group

    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 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

    算法思路:

    1. 判断字符串长length < k 则直接返回

    2. 将前k个节点截取下来,reverse,并找到reverse后的尾节点

    3. 递归,将后来生成的结果都插入到队列后面

     1 public class Solution {
     2 public ListNode reverseKGroup(ListNode head, int k) {
     3         if(head == null || k <= 1) return head;
     4         ListNode tail = head;
     5         int length = 1;
     6         while(tail.next != null){
     7             length++;
     8             tail = tail.next;
     9             if(length == k) break;
    10         }
    11         if(length < k) return head;//judge if the list need to process
    12         ListNode left = tail.next;
    13         tail.next = null;
    14         ListNode result = reverse(head);
    15         tail = result;
    16         while(tail.next != null){//find the tail node of the reversed list
    17             tail = tail.next;
    18         }
    19         tail.next = reverseKGroup(left, k);
    20         return result;
    21     }
    22     private ListNode reverse(ListNode head){
    23         ListNode pre = head;
    24         ListNode thus = head.next;
    25         ListNode post = thus.next;
    26         pre.next = null;
    27         while(thus != null){
    28             thus.next = pre;
    29             pre = thus;
    30             thus = post;
    31             if(post != null)post = post.next;
    32         }
    33         return pre;
    34     }
    35 }

    第二遍记录:

    看了上一遍的代码就没兴趣看了,采用头插法,并记录剩余节点个数,当剩余节点数 < k时,就不需要处理了。

     1 public class Solution {
     2     public ListNode reverseKGroup(ListNode head, int k) {
     3         if(head == null || head.next == null || k <= 0) return head;
     4         int length = 0;
     5         ListNode hhead = new ListNode(0);
     6         hhead.next = head;
     7         ListNode tail = head, p = hhead;
     8         for(; tail != null; tail = tail.next, length++);
     9         while(length >= k){
    10             ListNode newTail = p.next;
    11             for(int i = 0; i < k - 1; i++){
    12                 ListNode pointer = newTail.next;
    13                 newTail.next = pointer.next;
    14                 pointer.next = p.next;
    15                 p.next = pointer;
    16             }
    17             p = newTail;
    18             length -= k;
    19         }
    20         return hhead.next;
    21     }
    22 }

    FYI

  • 相关阅读:
    你不是不行只是对自己要求太低
    数学的思维方式
    python文档生成工具:pydoc、sphinx;django如何使用sphinx?
    python抽象类的实现方式:abc模块
    python的重试库tenacity用法以及类似库retry、requests实现
    dict扩展munch,支持yaml文件
    python读取yaml配置文件
    博客园的打赏功能、打赏插件
    junit5了解一下
    sulime代理设置、插件管理
  • 原文地址:https://www.cnblogs.com/huntfor/p/3859121.html
Copyright © 2011-2022 走看看