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

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

输入 : 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/

转载于:https://www.cnblogs.com/passedbylove/p/11442606.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值