原文:
给定一个链表,在不修改链表的情况下将其打印出来。
示例:
input : 1 2 3 4 5 6
output : 6 5 4 3 2 1
input : 12 23 34 45 56 67 78
output : 78 67 56 45 34 23 12
下面是现在允许在此处使用的不同pg电子试玩链接的解决方案,因为我们不能使用额外的空间来修改列表。
-
。 需要额外的空间。
-
,然后打印。 这需要对原始列表进行修改。
-
,首先计数节点,然后从末尾打印第
k
个节点。
在这篇文章中,讨论了有效的基于栈的pg电子试玩链接的解决方案。
-
首先,将所有元素插入栈
-
打印栈,直到栈不为空
注意:不要将每个节点的数据插入栈,而是将节点的地址插入栈。 这是因为节点数据的大小通常会大于节点地址的大小。 因此,如果栈直接存储数据元素,则最终将需要更多的内存。 另外,如果每个节点包含多个数据成员,则我们无法将节点的数据插入栈。 因此,一种简单有效的pg电子试玩链接的解决方案是简单地插入节点的地址。
以下是上述想法的实现:
c
// c/c program to print reverse of linked list
// using stack.
#include
using namespace std;
// link list node
struct node {
int data;
struct node* next;
};
// 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.
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;
}
// counts no. of nodes in linked list
int getcount(struct node* head)
{
int count = 0; // initialize count
struct node* current = head; // initialize current
while (current != null) {
count ;
current = current->next;
}
return count;
}
// takes head pointer of the linked list and index
// as arguments and return data at index
int getnth(struct node* head, int n)
{
struct node* curr = head;
for (int i = 0; i < n - 1 && curr != null; i )
curr = curr->next;
return curr->data;
}
void printreverse(node* head)
{
// store node addresses in stack
stack stk;
node* ptr = head;
while (ptr != null) {
stk.push(ptr);
ptr = ptr->next;
}
// print data from stack
while (!stk.empty()) {
cout << stk.top()->data << " ";
stk.pop(); // pop after print
}
cout << "\n";
}
// driver code
int main()
{
// start with the empty list
struct node* head = null;
// use push() to construct below list
// 1->2->3->4->5
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
// function call
printreverse(head);
return 0;
}
java
// java program to print reverse of linked list
// using stack.
import java.util.*;
class gfg {
/* link list node */
static class node {
int data;
node next;
};
/* 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 node push(node head_ref, int new_data)
{
node new_node = new node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
/* counts no. of nodes in linked list */
static int getcount(node head)
{
int count = 0; // initialize count
node current = head; // initialize current
while (current != null) {
count ;
current = current.next;
}
return count;
}
/* takes head pointer of the linked list and index
as arguments and return data at index*/
static int getnth(node head, int n)
{
node curr = head;
for (int i = 0; i < n - 1 && curr != null; i )
curr = curr.next;
return curr.data;
}
static void printreverse(node head)
{
// store node addresses in stack
stack stk = new stack();
node ptr = head;
while (ptr != null) {
stk.push(ptr);
ptr = ptr.next;
}
// print data from stack
while (stk.size() > 0) {
system.out.print(stk.peek().data " ");
stk.pop(); // pop after print
}
system.out.println("\n");
}
// driver code
public static void main(string args[])
{
// start with the empty list
node head = null;
// use push() to con below list
// 1.2.3.4.5
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
// function call
printreverse(head);
}
}
// this code is contributed by arnab kundu
python3
# python3 program to print reverse of linked list
# using stack.
# node of a linked list
class node:
def __init__(self, next=none, data=none):
self.next = next
self.data = data
# 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.
def push(head_ref, new_data):
new_node = node()
new_node.data = new_data
new_node.next = (head_ref)
(head_ref) = new_node
return head_ref
# counts no. of nodes in linked list
def getcount(head):
count = 0 # initialize count
current = head # initialize current
while (current != none):
count = count 1
current = current.next
return count
# takes head pointer of the linked list and index
# as arguments and return data at index
def getnth(head, n):
curr = head
i = 0
while(i < n - 1 and curr != none):
curr = curr.next
i = i 1
return curr.data
def printreverse(head):
# store node addresses in stack
stk = []
ptr = head
while (ptr != none):
stk.append(ptr)
ptr = ptr.next
# print data from stack
while (len(stk) > 0):
print(stk[-1].data, end=" ")
stk.pop() # pop after print
print(" ")
# driver code
# start with the empty list
head = none
# use push() to con below list
# 1.2.3.4.5
head = push(head, 5)
head = push(head, 4)
head = push(head, 3)
head = push(head, 2)
head = push(head, 1)
# function call
printreverse(head)
# this code is contributed by arnab kundu
c
// c# program to print reverse of linked list
// using stack.
using system;
using system.collections.generic;
class gfg {
/* link list node */
public class node {
public int data;
public node next;
};
/* 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 node push(node head_ref, int new_data)
{
node new_node = new node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
/* counts no. of nodes in linked list */
static int getcount(node head)
{
int count = 0; // initialize count
node current = head; // initialize current
while (current != null) {
count ;
current = current.next;
}
return count;
}
/* takes head pointer of the linked list and index
as arguments and return data at index*/
static int getnth(node head, int n)
{
node curr = head;
for (int i = 0; i < n - 1 && curr != null; i )
curr = curr.next;
return curr.data;
}
static void printreverse(node head)
{
// store node addresses in stack
stack stk = new stack();
node ptr = head;
while (ptr != null) {
stk.push(ptr);
ptr = ptr.next;
}
// print data from stack
while (stk.count > 0) {
console.write(stk.peek().data " ");
stk.pop(); // pop after print
}
console.writeline("\n");
}
// driver code
public static void main(string[] args)
{
// start with the empty list
node head = null;
// use push() to con below list
// 1.2.3.4.5
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
// function call
printreverse(head);
}
}
// this code is contributed by rajput-ji
输出:
5 4 3 2 1
如果您喜欢 geeksforgeeks 并希望做出贡献,则还可以使用 tribution.geeksforgeeks.org 撰写文章,或将您的文章邮寄至 tribution@geeksforgeeks.org。 查看您的文章出现在 geeksforgeeks pg电子试玩链接主页上,并帮助其他 geeks。
如果您发现任何不正确的地方,请单击下面的“改进文章”按钮,以改进本文。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处