📌  相关文章
📜  用于反转给定大小组中的链表的Java程序集 2

📅  最后修改于: 2022-05-13 01:54:35.524000             🧑  作者: Mango

用于反转给定大小组中的链表的Java程序集 2

给定一个链表,编写一个函数来反转每 k 个节点(其中 k 是函数的输入)。
例子:

Input: 1->2->3->4->5->6->7->8->NULL and k = 3 
Output: 3->2->1->6->5->4->8->7->NULL. 

Input: 1->2->3->4->5->6->7->8->NULL and k = 5
Output: 5->4->3->2->1->8->7->6->NULL.

我们已经在下面的帖子中讨论了它的解决方案
在给定大小的组中反转链接列表 |设置 1
在这篇文章中,我们使用了一个堆栈来存储给定链表的节点。首先,将链表的 k 个元素压入栈中。现在一个一个地弹出元素并跟踪之前弹出的节点。将 prev 节点的 next 指针指向栈顶元素。重复此过程,直到达到 NULL。
该算法使用 O(k) 额外空间。

Java
// Java program to reverse a linked list 
// in groups of given size 
import java.util.*;
class GfG 
{
    // Link list node 
    static class Node 
    { 
        int data; 
        Node next; 
    }
    static Node head = null;
  
    /* Reverses the linked list in groups 
       of size k and returns the pointer 
       to the new head node. */
    static Node Reverse(Node head, int k) 
    { 
        // Create a stack of Node* 
        Stack mystack = 
                    new Stack (); 
        Node current = head; 
        Node prev = null; 
  
        while (current != null)
        {
            // Terminate the loop whichever 
            // comes first either current == NULL 
            // or count >= k 
            int count = 0; 
            while (current != null && count < k)
            { 
                mystack.push(current); 
                current = current.next; 
                count++; 
            } 
  
            // Now pop the elements of stack 
            // one by one 
            while (mystack.size() > 0) 
            { 
                // If final list has not been 
                // started yet. 
                if (prev == null)
                { 
                    prev = mystack.peek(); 
                    head = prev; 
                    mystack.pop(); 
                } 
                else
                { 
                    prev.next = mystack.peek(); 
                    prev = prev.next; 
                    mystack.pop(); 
                } 
            } 
        } 
  
        // Next of last element will point
        // to NULL. 
        prev.next = null; 
  
        return head; 
    } 
  
    // UTILITY FUNCTIONS 
    // Function to push a node 
    static void push( int new_data) 
    { 
        // Allocate node 
        Node new_node = new Node(); 
  
        // Put in the data 
        new_node.data = new_data; 
  
        // Link the old list off the 
        // new node 
        new_node.next = head; 
  
        // Move the head to point to 
        // the new node 
        head = new_node; 
    } 
  
    // Function to print linked list 
    static void printList(Node node) 
    { 
        while (node != null) 
        { 
            System.out.print(node.data + " "); 
            node = node.next; 
        } 
    } 
  
    // Driver code
    public static void main(String[] args) 
    { 
        // Start with the empty list 
        //Node head = null; 
  
        /* Created Linked list is 
           1->2->3->4->5->6->7->8->9 */
        push( 9); 
        push( 8); 
        push( 7); 
        push( 6); 
        push( 5); 
        push(4); 
        push(3); 
        push(2); 
        push( 1); 
  
        System.out.println(
               "Given linked list "); 
        printList(head); 
        head = Reverse(head, 3);
        System.out.println();
  
        System.out.println(
               "Reversed Linked list "); 
        printList(head); 
    } 
} 
// This code is contributed by Prerna Saini


输出:

Given Linked List
1 2 3 4 5 6 7 8 9 
Reversed list
3 2 1 6 5 4 9 8 7

请参阅完整的文章在给定大小的组中反转链接列表 |设置2了解更多详情!