zoukankan      html  css  js  c++  java
  • 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 /**
     2  * Definition for singly-linked list.
     3  * public class ListNode {
     4  *     int val;
     5  *     ListNode next;
     6  *     ListNode(int x) {
     7  *         val = x;
     8  *         next = null;
     9  *     }
    10  * }
    11  */
    12 public class Solution {
    13     public ListNode reverseKGroup(ListNode head, int k) {
    14         // Start typing your Java solution below
    15         // DO NOT write main() function
    16         if(k<=1 || head==null || head.next==null) return head;
    17         
    18         ListNode prev = new ListNode(0);
    19         prev.next = head;
    20         head = prev;
    21         
    22         ListNode cur=prev.next;
    23         while(cur!=null){
    24             int counter = k;
    25             while(cur!=null && counter>1){
    26                 cur = cur.next;
    27                 counter--;
    28             }
    29             
    30             if(cur!=null){
    31                 cur=prev.next;
    32                 counter=k;
    33                 while(counter>1){
    34                     ListNode temp = cur.next;
    35                     cur.next=temp.next;
    36                     temp.next=prev.next;
    37                     prev.next=temp;
    38                     counter--;
    39                 }
    40                 prev = cur;
    41                 cur = prev.next;
    42             }
    43         }
    44         
    45         return head.next;
    46     }
    47 }

     Second Round:

    Divide the original linked list into groups, while each group contains k elements. reverse this sublist. ---> do it as reverse linked list.

    It will be much easier to understand!!!

     1 /**
     2  * Definition for singly-linked list.
     3  * public class ListNode {
     4  *     int val;
     5  *     ListNode next;
     6  *     ListNode(int x) {
     7  *         val = x;
     8  *         next = null;
     9  *     }
    10  * }
    11  */
    12 public class Solution {
    13     public ListNode reverseKGroup(ListNode head, int k) {
    14         if(head == null) return null;
    15         ListNode header = new ListNode(-1);
    16         header.next = head;
    17         ListNode curTail = head, curHead = head, perTail = header;
    18         while(curHead != null){
    19             for(int i = 1; i < k; i ++){
    20                 curTail = curTail.next;
    21                 if(curTail == null){
    22                     perTail.next = curHead;
    23                     return header.next;
    24                 }
    25             }
    26             ListNode nextHead = curTail.next;
    27             curTail.next = null;
    28             ListNode newHead = reverseList(curHead);
    29             perTail.next = newHead;
    30             perTail = curHead;
    31             curHead = nextHead;
    32             curTail = nextHead;
    33         }
    34         return header.next;
    35     }
    36     
    37     public ListNode reverseList(ListNode head){
    38         ListNode per = null;
    39         ListNode cur = head;
    40         while(cur != null){
    41             ListNode tmp = cur.next;
    42             cur.next = per;
    43             per = cur;
    44             cur = tmp;
    45         }
    46         return per;
    47     }
    48 }

     第三遍:

     1 public class Solution {
     2     public ListNode reverseKGroup(ListNode head, int k) {
     3         ListNode header = new ListNode(-1);
     4         header.next = head;
     5         ListNode cur = header, fast = header;
     6         while(fast != null){
     7             for(int i = 0; i < k; i ++){
     8                 fast = fast.next;
     9                 if(fast == null) return header.next;
    10             }
    11             ListNode next = fast.next;
    12             fast.next = null;
    13             reverseList(cur.next);
    14             ListNode newCur = cur.next;
    15             cur.next.next = next;
    16             cur.next = fast;
    17             cur = newCur;
    18             fast = newCur;
    19         }
    20         return header.next;
    21     }
    22     
    23     public ListNode reverseList(ListNode head){
    24         ListNode cur = head, pre = head.next;
    25         while(pre != null){
    26             ListNode tmp = pre.next;
    27             pre.next = cur;
    28             cur = pre;
    29             pre = tmp;
    30         }
    31         return cur;
    32     }
    33 }
  • 相关阅读:
    单例模式
    简单的WPS二次开发脚本
    使用DevExpress改变WinForm皮肤(VS)
    步入DevExpress的使用(VS)
    设置PdfPTable与标题间的距离
    tar: Removing leading `/’ from member names
    MySQL关闭过程详解和安全关闭MySQL的方法
    Nikto是一款Web安全扫描工具,可以扫描指定主机的web类型,主机名,特定目录,cookie,特定CGI漏洞,XSS漏洞,SQL注入漏洞等,非常强大滴说。。。
    解决Centos关闭You have new mail in /var/spool/mail/root提示
    hping3命令
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/3370140.html
Copyright © 2011-2022 走看看