原文:
给定,以反向方式显示链表,而无需使用递归,栈或对给定列表的修改。
示例:
input : 1->2->3->4->5->null
output :5->4->3->2->1->null
input :10->5->15->20->24->null
output :24->20->15->5->10->null
下面是现在允许在此处使用的不同pg电子试玩链接的解决方案,因为我们不能使用额外的空间并修改列表。
-
。 需要额外的空间。
-
,然后打印。 这需要修改原始列表。
-
。 将所有节点一个一个地推入栈。 然后从栈中逐一弹出元素并进行打印。 这也需要额外的空间。
算法:
1) find n = count nodes in linked list.
2) for i = n to 1, do following.
print i-th node using get n-th node function
c
// c/c program to print reverse of linked list
// without extra space and without modifications.
#include
#include
/* 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; inext;
return curr->data;
}
void printreverse(node *head)
{
// count nodes
int n = getcount(head);
for (int i=n; i>=1; i--)
printf("%d ", getnth(head, i));
}
/* driver program to test count function*/
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);
printreverse(head);
return 0;
}
java
// java program to print reverse of linked list
// without extra space and without modifications.
class gfg
{
/* link list node */
static class node
{
int data;
node next;
};
static node head;
/* 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(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;
head = 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()
{
// count nodes
int n = getcount(head);
for (int i = n; i >= 1; i--)
system.out.printf("%d ", getnth(head, i));
}
// driver code
public static void main(string[] args)
{
/* start with the empty list */
head = null;
/* use push() to conbelow list
1->2->3->4->5 */
push(head, 5);
push(head, 4);
push(head, 3);
push(head, 2);
push(head, 1);
printreverse();
}
}
// this code is contributed by princiraj1992
c
// c# program to print reverse of
// linked list without extra space
// and without modifications.
using system;
class gfg
{
/* link list node */
public class node
{
public int data;
public node next;
};
static node head;
/* 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(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;
head = 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()
{
// count nodes
int n = getcount(head);
for (int i = n; i >= 1; i--)
console.write("{0} ", getnth(head, i));
}
// driver code
public static void main(string[] args)
{
/* start with the empty list */
head = null;
/* use push() to conbelow list
1->2->3->4->5 */
push(head, 5);
push(head, 4);
push(head, 3);
push(head, 2);
push(head, 1);
printreverse();
}
}
// this code is contributed by princi singh
输出:
5 4 3 2 1
如果您喜欢 geeksforgeeks 并希望做出贡献,则还可以使用 tribution.geeksforgeeks.org 撰写文章,或将您的文章邮寄至 tribution@geeksforgeeks.org。 查看您的文章出现在 geeksforgeeks pg电子试玩链接主页上,并帮助其他 geeks。
如果您发现任何不正确的地方,请单击下面的“改进文章”按钮,以改进本文。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处