zoukankan      html  css  js  c++  java
  • 链表k个节点反向

    问题:

    以k个元素为一组,反转单向链表。比如:

    输入: 1->2->3->4->5->6->7->8->null and k = 3

    输出:3->2->1->6->5->4->8->7->null. 

    分析:

    我们可以把整个链表分成多个长度为 k  的子链表, 然后,我们再反转每一个子链表(递归)。问题的关键是我们需要把每个子链表再连接起来。所以,对每一个子链表操作以后,我们需要返回该子链表的头(head),然后,我们设置前一个子链表的最后一个node,把它的next 设置成下一个链表返回的头(head),这样,所有的子链表就连接起来了。

     

    1. public static Node reverse (Node head, int k) {  
    2.     Node current = head;  
    3.     Node next = null;  
    4.     Node prev = null;  
    5.     int count = 0;     
    6.       
    7.     /*reverse first k nodes of the linked list */  
    8.     while (current != null && count < k) {  
    9.        next  = current.next;  
    10.        current.next = prev;  
    11.        prev = current;  
    12.        current = next;  
    13.        count++;  
    14.     }  
    15.    
    16.     /* next is now a pointer to (k+1)th node 
    17.        Recursively call for the list starting from current. 
    18.        And make rest of the list as next of first node */  
    19.     if(next !=  null) {  
    20.         head.next = reverse(next, k);   
    21.     }  
    22.    
    23.     /* prev is new head of the input list */  
    24.     return prev;  
    25. }  
    public static Node reverse (Node head, int k) {
    	Node current = head;
    	Node next = null;
    	Node prev = null;
        int count = 0;   
        
        /*reverse first k nodes of the linked list */
        while (current != null && count < k) {
           next  = current.next;
           current.next = prev;
           prev = current;
           current = next;
           count++;
        }
     
        /* next is now a pointer to (k+1)th node
           Recursively call for the list starting from current.
           And make rest of the list as next of first node */
        if(next !=  null) {
        	head.next = reverse(next, k); 
        }
     
        /* prev is new head of the input list */
        return prev;
    }

     

    这个问题也可以使用非递归的方法,基本上问题的处理方式和递归是一样的,但是,非递归的方式要稍微复杂一点,因为每次对子链表反转以后,我们需要更新前一个子链表最后一个node 的next 值。代码如下:

    1. class Node {  
    2.      int val;  
    3.      Node next;  
    4.      Node(int x) {  
    5.          val = x;  
    6.          next = null;  
    7.      }  
    8. }  
    9.   
    10. public class Solution {  
    11.       
    12.     public static void main(String[] args) {  
    13.         Solution s = new Solution();  
    14.           
    15.         Node n1 = new Node(1);  
    16.         Node n2 = new Node(2);  
    17.         Node n3 = new Node(3);  
    18.         Node n4 = new Node(4);  
    19.         Node n5 = new Node(5);  
    20.           
    21.         n1.next = n2;  
    22.         n2.next = n3;  
    23.         n3.next = n4;  
    24.         n4.next = n5;  
    25.           
    26.         Node head = s.ReverseInGroups(n1, 2);  
    27.         while (head != null) {  
    28.             System.out.println(head.val);  
    29.             head = head.next;  
    30.         }  
    31.     }  
    32.     public Node ReverseInGroups(Node current, int k) {  
    33.         if (current == null || current.next == null ) return current;  
    34.         //store the new head of the list  
    35.         Node newHead = null;  
    36.           
    37.         //store the last node in the sub-list,   
    38.         //we will update its next value when finishing  
    39.         //reversing the next sub-list  
    40.         Node previousGroupTail = null;  
    41.         int count = 1; //used to track the first sub-list  
    42.         while (current != null) {     
    43.             // the actual tail in the sub-list  
    44.             Node groupTail = current;  
    45.             //reverse  
    46.             Node prev = null;  
    47.             Node next = null;  
    48.             for (int i = 1; i <= k && current != null; i++) {  
    49.                 next = current.next;  
    50.                 current.next = prev;  
    51.                 prev = current;  
    52.                 current = next;  
    53.             }  
    54.             // get the new head of the whole list  
    55.             if (count == 1) {  
    56.                 newHead = prev;  
    57.                 count++;  
    58.             }  
    59.             // update the next value of the last node in  
    60.             // each sub-list  
    61.             if (previousGroupTail != null) {  
    62.                 previousGroupTail.next = prev;  
    63.             }  
    64.             previousGroupTail = groupTail;  
    65.         }  
    66.         return newHead;  
    67.     }  
    68. }  
  • 相关阅读:
    java——异常(一)
    java —— 全面解析 Annotation
    多线程(一)——三种实现方式
    java ——static 关键词总结
    java —— equals 与 ==
    java——数组与内存控制
    java—— finall 关键词
    File类实现文件夹和文件复制
    java —— 内部类
    类成员——代码块
  • 原文地址:https://www.cnblogs.com/mascotxi/p/4402139.html
Copyright © 2011-2022 走看看