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

  • 相关阅读:
    处理字符串拼接成想要的数组
    json 压缩中文不转码
    MySQL主从同步机制及同步中的问题处理
    mysql主从复制亲测,以及注意事项
    windows下mysql和linux下mysql主从配置
    拯救U盘之——轻松修复U盘“无法访问”的故障
    MUI框架开发HTML5手机APP(一)--搭建第一个手机APP
    UI之富文本编辑器-UEditor
    弹性布局详解——5个div让你学会弹性布局
    内置函数:min 用法
  • 原文地址:https://www.cnblogs.com/huntfor/p/3859121.html
Copyright © 2011-2022 走看看