• 数据结构:使用栈对链表元素位序进行反转


    给定一个链表,业务需求:使用栈将链表中元素的次序进行反转。

    输入 : List = 3 -> 2 -> 1
    输出 : 1 -> 2 -> 3
    
    输入 : 9 -> 7 -> 4 -> 2
    输出 : 2 -> 4 -> 7 -> 9 



    算法过程:
    算法复杂度:O(n)
    1、遍历列表,将所有节点推到栈上。
    2、遍历栈,并依次从栈顶弹出元素用相反顺序存储。
    java代码:
    // Java program to reverse linked list 
    // using stack 
    import java.util.*; 
    class GfG 
    { 
    
    /* Link list node */
    static class Node 
    { 
        int data; 
        Node next; 
    } 
    static Node head = null; 
    
    /* Given a reference (pointer to pointer) to 
    the head of a list and an int, push a new 
    node on the front of the list. */
    static void push( int new_data) 
    { 
        Node new_node = new Node(); 
    
        new_node.data = new_data; 
        new_node.next = (head); 
        (head) = new_node; 
    } 
    
    // Function to reverse linked list 
    static Node reverseList(Node head) 
    { 
        // Stack to store elements of list 
        Stack<Node > stk = new Stack<Node> (); 
    
        // Push the elements of list to stack 
        Node ptr = head; 
        while (ptr.next != null) 
        { 
            stk.push(ptr); 
            ptr = ptr.next; 
        } 
    
        // Pop from stack and replace 
        // current nodes value' 
        head = ptr; 
        while (!stk.isEmpty()) 
        { 
            ptr.next = stk.peek(); 
            ptr = ptr.next; 
            stk.pop(); 
        } 
        ptr.next = null; 
        
        return head; 
    } 
    
    // Function to print the Linked list 
    static void printList(Node head) 
    { 
        while (head != null) 
        { 
            System.out.print(head.data + " "); 
            head = head.next; 
        } 
    } 
    
    // Driver Code 
    public static void main(String[] args) 
    { 
        /* Start with the empty list */
        //Node head = null; 
    
        /* Use push() to construct below list 
        1->2->3->4->5 */
        push( 5); 
        push( 4); 
        push( 3); 
        push( 2); 
        push( 1); 
    
        head = reverseList(head); 
    
        printList(head); 
    } 
    } 
    
    // This code is contributed by Prerna Saini. 

    c#代码

    // C# program to reverse linked list 
    // using stack 
    using System; 
    using System.Collections.Generic; 
    
    class GfG 
    { 
    
    /* Link list node */
    public class Node 
    { 
        public int data; 
        public Node next; 
    } 
    static Node head = null; 
    
    /* Given a reference (pointer to pointer) to 
    the head of a list and an int, push a new 
    node on the front of the list. */
    static void push( int new_data) 
    { 
        Node new_node = new Node(); 
    
        new_node.data = new_data; 
        new_node.next = (head); 
        (head) = new_node; 
    } 
    
    // Function to reverse linked list 
    static Node reverseList(Node head) 
    { 
        // Stack to store elements of list 
        Stack<Node > stk = new Stack<Node> (); 
    
        // Push the elements of list to stack 
        Node ptr = head; 
        while (ptr.next != null) 
        { 
            stk.Push(ptr); 
            ptr = ptr.next; 
        } 
    
        // Pop from stack and replace 
        // current nodes value' 
        head = ptr; 
        while (stk.Count != 0) 
        { 
            ptr.next = stk.Peek(); 
            ptr = ptr.next; 
            stk.Pop(); 
        } 
        ptr.next = null; 
        
        return head; 
    } 
    
    // Function to print the Linked list 
    static void printList(Node head) 
    { 
        while (head != null) 
        { 
            Console.Write(head.data + " "); 
            head = head.next; 
        } 
    } 
    
    // Driver Code 
    public static void Main(String[] args) 
    { 
        /* Start with the empty list */
        //Node head = null; 
    
        /* Use push() to construct below list 
        1->2->3->4->5 */
        push( 5); 
        push( 4); 
        push( 3); 
        push( 2); 
        push( 1); 
    
        head = reverseList(head); 
    
        printList(head); 
    } 
    } 
    
    // This code contributed by Rajput-Ji 

    算法2(迭代):

    时间复杂度:O(1)

    // Iterative C program to reverse a linked list
    #include <stdio.h>
    #include <stdlib.h>
    
    /* Linked list node */
    struct Node
    {
        int data;
        struct Node* next;
    };
    
    //function to reverse the linked list
    static void reverse(struct Node** head_ref)
    {
        struct Node* prev = NULL;
        struct Node* current = *head_ref;
        struct Node* next = NULL;
    
        while(current != NULL)
        {
            //Store next
            next = current->next;
    
            //Move pointers one position ahead.
            prev = current;
    
            current = next;
        }
        *head_ref = prev;
    }
    
    /* function to push a node */
    void push(struct Node** head_ref, int new_data)
    {
        struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
        new_node->data = new_data;
        new_node->next =(*head_ref);
        (*head_ref) = new_node;
    }
    
    /* function to print linked list */
    void printList(struct Node* head)
    {
        struct Node* temp = head;
        while( temp != NULL)
        {
            printf("%d ", temp->data);
            temp = temp->next;
        }
        printf("
    ");
    }
    
    /* Driver program to test above function */
    int main()
    {
        /* Start with the empty list */
        struct Node* head = NULL;
    
        push(&head, 20);
        push(&head, 4);
        push(&head, 15);
        push(&head, 85);
    
        printf("Given linked list
    ");
        printList(head);
    
        reverse(&head);
    
        printf("Reversed Linked List
    ");
        printList(head);
    }

    文章来源:https://www.geeksforgeeks.org/program-to-reverse-a-linked-list-using-stack/

  • 相关阅读:
    Windows开发,关于通过写代码加载PDB的那些事
    从FreeBSD里面看到的网络协议列表,感觉可以保存一下
    听了几段《双投唐》
    ReactOS 无法显示中文的问题
    ReactOS 代码更新后的编译安装
    都什么年代了,怎么还那种德行
    假冒不伪劣
    ollvm 使用——“Cannot open /dev/random”错误的解决方法
    ollvm 编译
    C/C++ 吐槽第一期:你最讨厌的C/C++里面的数据类型是什么
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11442606.html
Copyright © 2020-2023  润新知