数据结构:使用栈对链表元素位序进行反转
Posted passedbylove
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构:使用栈对链表元素位序进行反转相关的知识,希望对你有一定的参考价值。
给定一个链表,业务需求:使用栈将链表中元素的次序进行反转。
输入 : 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("\n"); /* 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\n"); printList(head); reverse(&head); printf("Reversed Linked List\n"); printList(head);
文章来源:https://www.geeksforgeeks.org/program-to-reverse-a-linked-list-using-stack/
以上是关于数据结构:使用栈对链表元素位序进行反转的主要内容,如果未能解决你的问题,请参考以下文章