logo

Zvrátit propojený seznam

Daný ukazatel na hlavní uzel propojeného seznamu je úkolem obrátit propojený seznam. Musíme obrátit seznam změnou vazeb mezi uzly.

Příklady :



Vstup : Vedoucí následujícího propojeného seznamu
1->2->3->4->NULL
Výstup : Propojený seznam by měl být změněn na,
4->3->2->1->NULL

Vstup : Vedoucí následujícího propojeného seznamu
1->2->3->4->5->NULL
Výstup : Propojený seznam by měl být změněn na,
5->4->3->2->1->NULL

Vstup : NULA
Výstup : NULA



Vstup : 1->NULL
Výstup : 1->NULL

Doporučený postup Obrátit propojený seznam Zkuste to!

Obrátit propojený seznam iterační metodou:

Cílem je použít tři ukazatele curr , předchozí, a další sledovat uzly pro aktualizaci zpětných odkazů.

Při řešení problému postupujte podle následujících kroků:



  • Inicializujte tři ukazatele předchozí jako NULL, curr tak jako hlava , a další jako NULL.
  • Iterujte propojený seznam. Ve smyčce proveďte následující:
    • Před změnou další z curr , uložit další uzel
      • další = curr -> další
    • Nyní aktualizujte další ukazatel na curr k předchozí
      • curr -> next = předchozí
    • Aktualizace předchozí tak jako curr a curr tak jako další
      • předchozí = aktuální
      • curr = další

Níže je uvedena implementace výše uvedeného přístupu:

C++
// Iterative C++ program to reverse a linked list #include  using namespace std; /* Link list node */ struct Node {  int data;  struct Node* next;  Node(int data)  {  this->data = data;  další = NULL;  } }; struct LinkedList { Node* head;  LinkedList() { head = NULL; } /* Funkce pro obrácení propojeného seznamu */ void reverse() { // Inicializace aktuálního, předchozího a následujícího ukazatele Uzel* current = head;  Uzel *předchozí = NULL, *další = NULL;  while (aktuální != NULL) { // Uložit další další = aktuální->další;  // Převrácení aktuálního ukazatele uzlu current->next = prev;  // Přesune ukazatele o jednu pozici dopředu.  předchozí = aktuální;  aktuální = další;  } hlava = předchozí;  } /* Funkce pro tisk propojeného seznamu */ void print() { struct Node* temp = head;  while (temp != NULL) { cout<< temp->data<< ' ';  temp = temp->další;  } } void push(int data) { Node* temp = new Node(data);  temp->další = hlava;  hlava = teplota;  } }; /* Kód ovladače*/ int main() { /* Začněte s prázdným seznamem */ LinkedList ll;  ll.push(20);  ll.push(4);  ll.push(15);  ll.push(85);  cout<< 'Given linked list
';  ll.print();  ll.reverse();  cout << '
Reversed linked list 
';  ll.print();  return 0; }>
C
// Iterative C program to reverse a linked list #include  #include  /* Link 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->další;  // Převrácení aktuálního ukazatele uzlu current->next = prev;  // Přesune ukazatele o jednu pozici dopředu.  předchozí = aktuální;  aktuální = další;  } *head_ref = předchozí; } /* Funkce pro odeslání uzlu */ void push(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));  nový_uzel->data = nová_data;  novy_uzel->dalsi = (*odkaz_hlavy);  (*head_ref) = nový_uzel; } /* Funkce pro tisk propojeného seznamu */ void printList(struct Node* head) { struct Node* temp = head;  while (temp != NULL) { printf('%d ', temp->data);  temp = temp->další;  } } /* Kód ovladače*/ int main() { /* Začněte s prázdným seznamem */ struct Node* head = NULL;  push(&head, 20);  push(&head, 4);  push(&head, 15);  push(&head, 85);  printf('Zadaný propojený seznam
');  printList(head);  obrátit(&head);  printf('
Obrácený propojený seznam 
');  printList(head);  getchar(); }>
Jáva
// Java program for reversing the linked list class LinkedList {  static Node head;  static class Node {  int data;  Node next;  Node(int d)  {  data = d;  next = null;  }  }  /* Function to reverse the linked list */  Node reverse(Node node)  {  Node prev = null;  Node current = node;  Node next = null;  while (current != null) {  next = current.next;  current.next = prev;  prev = current;  current = next;  }  node = prev;  return node;  }  // prints content of double linked list  void printList(Node node)  {  while (node != null) {  System.out.print(node.data + ' ');  node = node.next;  }  }  // Driver Code  public static void main(String[] args)  {  LinkedList list = new LinkedList();  list.head = new Node(85);  list.head.next = new Node(15);  list.head.next.next = new Node(4);  list.head.next.next.next = new Node(20);  System.out.println('Given linked list');  list.printList(head);  head = list.reverse(head);  System.out.println('');  System.out.println('Reversed linked list ');  list.printList(head);  } } // This code has been contributed by Mayank Jaiswal>
Krajta
# Python program to reverse a linked list # Time Complexity : O(n) # Space Complexity : O(1) # Node class class Node: # Constructor to initialize the node object def __init__(self, data): self.data = data self.next = None class LinkedList: # Function to initialize head def __init__(self): self.head = None # Function to reverse the linked list def reverse(self): prev = None current = self.head while(current is not None): next = current.next current.next = prev prev = current current = next self.head = prev # Function to insert a new node at the beginning def push(self, new_data): new_node = Node(new_data) new_node.next = self.head self.head = new_node # Utility function to print the LinkedList def printList(self): temp = self.head while(temp): print(temp.data, end=' ') temp = temp.next # Driver code llist = LinkedList() llist.push(20) llist.push(4) llist.push(15) llist.push(85) print ('Given linked list') llist.printList() llist.reverse() print ('
Reversed linked list') llist.printList() # This code is contributed by Nikhil Kumar Singh(nickzuck_007)>
C#
// C# program for reversing the linked list using System; class GFG {  // Driver Code  static void Main(string[] args)  {  LinkedList list = new LinkedList();  list.AddNode(new LinkedList.Node(85));  list.AddNode(new LinkedList.Node(15));  list.AddNode(new LinkedList.Node(4));  list.AddNode(new LinkedList.Node(20));  // List before reversal  Console.WriteLine('Given linked list ');  list.PrintList();  // Reverse the list  list.ReverseList();  // List after reversal  Console.WriteLine('Reversed linked list ');  list.PrintList();  } } class LinkedList {  Node head;  public class Node {  public int data;  public Node next;  public Node(int d)  {  data = d;  next = null;  }  }  // function to add a new node at  // the end of the list  public void AddNode(Node node)  {  if (head == null)  head = node;  else {  Node temp = head;  while (temp.next != null) {  temp = temp.next;  }  temp.next = node;  }  }  // function to reverse the list  public void ReverseList()  {  Node prev = null, current = head, next = null;  while (current != null) {  next = current.next;  current.next = prev;  prev = current;  current = next;  }  head = prev;  }  // function to print the list data  public void PrintList()  {  Node current = head;  while (current != null) {  Console.Write(current.data + ' ');  current = current.next;  }  Console.WriteLine();  } } // This code is contributed by Mayank Sharma>
Javascript
>

Výstup
Given linked list 85 15 4 20 Reversed linked list 20 4 15 85>

Časová náročnost: O(N), Přechod přes propojený seznam velikosti N.
Pomocný prostor: O(1)

Java vs C++

Zvrátit propojený seznam pomocí rekurze:

Cílem je dosáhnout posledního uzlu propojeného seznamu pomocí rekurze a poté začít obracet propojený seznam.

Při řešení problému postupujte podle následujících kroků:

  • Rozdělte seznam na dvě části – první uzel a zbytek propojeného seznamu.
  • Volejte zpětně pro zbytek propojeného seznamu.
  • Propojte zbývající propojený seznam s prvním.
  • Opravit ukazatel hlavy na NULL

Níže je implementace výše uvedeného přístupu:

C++
// Recursive C++ program to reverse // a linked list #include  using namespace std; /* Link list node */ struct Node {  int data;  struct Node* next;  Node(int data)  {  this->data = data;  další = NULL;  } }; struct LinkedList { Node* head;  LinkedList() { head = NULL; } Node* reverse(Node* head) /* Funkce pro tisk propojeného seznamu */ void print() { struct Node* temp = head;  while (temp != NULL) { cout<< temp->data<< ' ';  temp = temp->další;  } } void push(int data) { Node* temp = new Node(data);  temp->další = hlava;  hlava = teplota;  } }; /* Ovladač k otestování výše uvedené funkce*/ int main() { /* Začněte s prázdným seznamem */ LinkedList ll;  ll.push(20);  ll.push(4);  ll.push(15);  ll.push(85);  cout<< 'Given linked list
';  ll.print();  ll.head = ll.reverse(ll.head);  cout << '
Reversed linked list 
';  ll.print();  return 0; }>
Jáva
// Recursive Java program to reverse // a linked list import java.io.*; class recursion {  static Node head; // head of list  static class Node {  int data;  Node next;  Node(int d)  {  data = d;  next = null;  }  }  static Node reverse(Node head)    /* Function to print linked list */  static void print()  {  Node temp = head;  while (temp != null) {  System.out.print(temp.data + ' ');  temp = temp.next;  }  System.out.println();  }  static void push(int data)  {  Node temp = new Node(data);  temp.next = head;  head = temp;  }  /* Driver program to test above function*/  public static void main(String args[])  {  /* Start with the empty list */  push(20);  push(4);  push(15);  push(85);  System.out.println('Given linked list');  print();  head = reverse(head);  System.out.println('Reversed linked list');  print();  } } // This code is contributed by Prakhar Agarwal>
Krajta
'''Python3 program to reverse linked list using recursive method''' # Linked List Node class Node: def __init__(self, data): self.data = data self.next = None # Create and Handle list operations class LinkedList: def __init__(self): self.head = None # Head of list # Method to reverse the list def reverse(self, head): # If head is empty or has reached the list end if head is None or head.next is None: return head # Reverse the rest list rest = self.reverse(head.next) # Put first element at the end head.next.next = head head.next = None # Fix the header pointer return rest # Returns the linked list in display format def __str__(self): linkedListStr = '' temp = self.head while temp: linkedListStr = (linkedListStr + str(temp.data) + ' ') temp = temp.next return linkedListStr # Pushes new data to the head of the list def push(self, data): temp = Node(data) temp.next = self.head self.head = temp # Driver code linkedList = LinkedList() linkedList.push(20) linkedList.push(4) linkedList.push(15) linkedList.push(85) print('Given linked list') print(linkedList) linkedList.head = linkedList.reverse(linkedList.head) print('Reversed linked list') print(linkedList) # This code is contributed by Debidutta Rath>
C#
// Recursive C# program to // reverse a linked list using System; class recursion {  // Head of list  static Node head;  public class Node {  public int data;  public Node next;  public Node(int d)  {  data = d;  next = null;  }  }  static Node reverse(Node head)    if (head == null   // Function to print linked list  static void print()  {  Node temp = head;  while (temp != null) {  Console.Write(temp.data + ' ');  temp = temp.next;  }  Console.WriteLine();  }  static void push(int data)  {  Node temp = new Node(data);  temp.next = head;  head = temp;  }  // Driver code  public static void Main(String[] args)  {  // Start with the  // empty list  push(20);  push(4);  push(15);  push(85);  Console.WriteLine('Given linked list');  print();  head = reverse(head);  Console.WriteLine('Reversed linked list');  print();  } } // This code is contributed by gauravrajput1>
Javascript
>

Výstup
Given linked list 85 15 4 20 Reversed linked list 20 4 15 85>

Časová náročnost: O(N), návštěva nad každým uzlem jednou
Pomocný prostor: O(N), prostor zásobníku volání funkce

Zvrátit propojený seznam pomocí rekurzivní metody Tail:

Cílem je zachovat tři ukazatele předchozí , aktuální a další , rekurzivně navštíví každý uzel a vytvoří odkazy pomocí těchto tří ukazatelů.

Při řešení problému postupujte podle následujících kroků:

primitivní datové typy v jazyce Java
  • První aktualizace další s dalším uzlem aktuálního, tj. další = aktuální->další
  • Nyní vytvořte zpětný odkaz z aktuálního uzlu na předchozí uzel, tj. curr->next = předchozí
  • Pokud je navštívený uzel posledním uzlem, pak stačí vytvořit zpětný odkaz z aktuálního uzlu na předchozí uzel a aktualizovat hlavu.

Níže je uvedena implementace výše uvedeného přístupu:

C++
// A simple and tail recursive C++ program to reverse // a linked list #include  using namespace std; struct Node {  int data;  struct Node* next;  Node(int x) {  data = x;  next = NULL;  } }; void reverseUtil(Node* curr, Node* prev, Node** head); // This function mainly calls reverseUtil() // with prev as NULL void reverse(Node** head) {  if (!head)  return;  reverseUtil(*head, NULL, head); } // A simple and tail-recursive function to reverse // a linked list. prev is passed as NULL initially. void reverseUtil(Node* curr, Node* prev, Node** head) {  /* If last node mark it head*/  if (!curr->další) { *hlava = curr;  /* Aktualizace vedle předchozího uzlu */ curr->next = prev;  vrátit se;  } /* Uložit curr->next uzel pro rekurzivní volání */ Node* next = curr->next;  /* a update next ..*/ curr->next = prev;  reverseUtil(další, curr, head); } // Obslužná funkce pro tisk propojeného seznamu void printlist(Node* head) { while (head != NULL) { cout<< head->data<< ' ';  head = head->další;  } cout<< endl; } // Driver code int main() {  Node* head1 = new Node(1);  head1->další = nový uzel(2);  hlava1->dalsi->dalsi = new Node(3);  hlava1->dalsi->dalsi->dalsi = new Node(4);  head1->next->next->next->next = new Node(5);  head1->next->next->next->next->next = new Node(6);  head1->next->next->next->next->next->next = new Node(7);  head1->next->next->next->next->next->next->next = new Node(8);  cout<< 'Given linked list
';  printlist(head1);  reverse(&head1);  cout << 'Reversed linked list
';  printlist(head1);  return 0; }>
C
// A simple and tail recursive C program to reverse a linked // list #include  #include  typedef struct Node {  int data;  struct Node* next; } Node; void reverseUtil(Node* curr, Node* prev, Node** head); // This function mainly calls reverseUtil() // with prev as NULL void reverse(Node** head) {  if (!head)  return;  reverseUtil(*head, NULL, head); } // A simple and tail-recursive function to reverse // a linked list. prev is passed as NULL initially. void reverseUtil(Node* curr, Node* prev, Node** head) {  /* If last node mark it head*/  if (!curr->další) { *hlava = curr;  /* Aktualizace vedle předchozího uzlu */ curr->next = prev;  vrátit se;  } /* Uložit curr->next uzel pro rekurzivní volání */ Node* next = curr->next;  /* a update next ..*/ curr->next = prev;  reverseUtil(další, curr, head); } // Obslužná funkce pro vytvoření nového uzlu Node* newNode(int key) { Node* temp = (Node*)malloc(sizeof(Node));  temp->data = klíč;  temp->dalsi = NULL;  návratová teplota; } // Obslužná funkce pro tisk propojeného seznamu void printlist(Node* head) { while (head != NULL) { printf('%d ', head->data);  hlava = hlava->další;  } printf('
'); } // Kód ovladače int main() { Node* head1 = newNode(1);  hlava1->dalsi = novyUzel(2);  hlava1->dalsi->dalsi = novyUzel(3);  hlava1->dalsi->dalsi->dalsi = novyUzel(4);  hlava1->dalsi->dalsi->dalsi->dalsi = novyUzel(5);  hlava1->dalsi->dalsi->dalsi->dalsi->dalsi = newNode(6);  head1->next->next->next->next->next->next = newNode(7);  head1->next->next->next->next->next->next->next = newNode(8);  printf('Zadaný propojený seznam
');  printlist(head1);  obrátit(&hlava1);  printf('Obrácený propojený seznam
');  printlist(head1);  návrat 0; } // Tento kód přispěl Aditya Kumar (adityakumar129)>
Jáva
// Java program for reversing the Linked list class LinkedList {  static Node head;  static class Node {  int data;  Node next;  Node(int d)  {  data = d;  next = null;  }  }  // A simple and tail recursive function to reverse  // a linked list. prev is passed as NULL initially.  Node reverseUtil(Node curr, Node prev)  {  /*If head is initially null OR list is empty*/  if (head == null)  return head;  /* If last node mark it head*/  if (curr.next == null) {  head = curr;  /* Update next to prev node */  curr.next = prev;  return head;  }  /* Save curr->další uzel pro rekurzivní volání */ Uzel další1 = curr.next;  /* a aktualizovat další ..*/ curr.next = předchozí;  reverseUtil(dalsi1, curr);  vratná hlava;  } // vypíše obsah dvojitě propojeného seznamu void printList(Node node) { while (node ​​!= null) { System.out.print(node.data + ' ');  uzel = uzel.dalsi;  } } // Kód ovladače public static void main(String[] args) { Seznam LinkedList = new LinkedList();  list.head = new Node(1);  list.head.next = new Node(2);  list.head.next.next = new Node(3);  list.head.next.next.next = new Node(4);  list.head.next.next.next.next = new Node(5);  list.head.next.next.next.next.next = new Node(6);  list.head.next.next.next.next.next.next = new Node(7);  list.head.next.next.next.next.next.next.next = new Node(8);  System.out.println('Zadaný seznam odkazů ');  seznam.tiskSeznam(hlava);  Node res = list.reverseUtil(head, null);  System.out.println('
Obrácený seznam odkazů ');  list.printList(res);  } } // Tento kód přidal Aditya Kumar (adityakumar129)>
Krajta
# Simple and tail recursive Python program to # reverse a linked list # Node class class Node: # Constructor to initialize the node object def __init__(self, data): self.data = data self.next = None class LinkedList: # Function to initialize head def __init__(self): self.head = None def reverseUtil(self, curr, prev): # If last node mark it head if curr.next is None: self.head = curr # Update next to prev node curr.next = prev return # Save curr.next node for recursive call next = curr.next # And update next curr.next = prev self.reverseUtil(next, curr) # This function mainly calls reverseUtil() # with previous as None def reverse(self): if self.head is None: return self.reverseUtil(self.head, None) # Function to insert a new node at the beginning def push(self, new_data): new_node = Node(new_data) new_node.next = self.head self.head = new_node # Utility function to print the linked LinkedList def printList(self): temp = self.head while(temp): print (temp.data, end=' ') temp = temp.next # Driver code llist = LinkedList() llist.push(8) llist.push(7) llist.push(6) llist.push(5) llist.push(4) llist.push(3) llist.push(2) llist.push(1) print ('Given linked list') llist.printList() llist.reverse() print ('
Reversed linked list') llist.printList() # This code is contributed by Nikhil Kumar Singh(nickzuck_007)>
C#
// C# program for reversing the Linked list using System; public class LinkedList {  Node head;  public class Node {  public int data;  public Node next;  public Node(int d)  {  data = d;  next = null;  }  }  // A simple and tail-recursive function to reverse  // a linked list. prev is passed as NULL initially.  Node reverseUtil(Node curr, Node prev)  {  /* If last node mark it head*/  if (curr.next == null) {  head = curr;  /* Update next to prev node */  curr.next = prev;  return head;  }  /* Save curr->další uzel pro rekurzivní volání */ Uzel další1 = curr.next;  /* a aktualizovat další ..*/ curr.next = předchozí;  reverseUtil(dalsi1, curr);  vratná hlava;  } // vypíše obsah dvojitě propojeného seznamu void printList(Node node) { while (node ​​!= null) { Console.Write(node.data + ' ');  uzel = uzel.dalsi;  } } // Kód ovladače public static void Main(String[] args) { Seznam LinkedList = new LinkedList();  list.head = new Node(1);  list.head.next = new Node(2);  list.head.next.next = new Node(3);  list.head.next.next.next = new Node(4);  list.head.next.next.next.next = new Node(5);  list.head.next.next.next.next.next = new Node(6);  list.head.next.next.next.next.next.next = new Node(7);  list.head.next.next.next.next.next.next.next = new Node(8);  Console.WriteLine('Zadaný seznam odkazů ');  seznam.tiskSeznam(seznam.hlava);  Node res = list.reverseUtil(list.head, null);  Console.WriteLine('
Obrácený seznam odkazů ');  list.printList(res);  } } // Tento kód přispěl Rajput-Ji>
Javascript
>

Výstup
Given linked list 1 2 3 4 5 6 7 8 Reversed linked list 8 7 6 5 4 3 2 1>

Časová náročnost: O(N), návštěva každého uzlu propojeného seznamu velikosti N.
Pomocný prostor: O(N), prostor zásobníku volání funkce

Obrátit propojený seznam pomocí Cílem je uložit všechny uzly do zásobníku a poté vytvořit seznam obrácených odkazů.

Při řešení problému postupujte podle následujících kroků:

  • Uložte uzly (hodnoty a adresy) do zásobníku, dokud nezadáte všechny hodnoty.
  • Po dokončení všech položek aktualizujte ukazatel Head na poslední místo (tj. na poslední hodnotu).
  • Začněte objevovat uzly (hodnotu a adresu) a ukládejte je ve stejném pořadí, dokud nebude zásobník prázdný.
  • Aktualizujte další ukazatel posledního uzlu v zásobníku hodnotou NULL.

Níže je uvedena implementace výše uvedeného přístupu:

C++
// C++ program for above approach #include  #include  using namespace std; // Create a class Node to enter values and address in the // list class Node { public:  int data;  Node* next;  Node(int x) {  data = x;  next = NULL;  } }; // Function to reverse the linked list void reverseLL(Node** head) {  // Create a stack 's' of Node type  stacks;  Node* temp = *head;  while (temp->next != NULL) { // Vložení všech uzlů do zásobníku s.push(temp);  temp = temp->další;  } *hlava = teplota;  while (!s.empty()) { // Uložení nejvyšší hodnoty zásobníku do seznamu temp->next = s.top();  // Vyber hodnotu ze zásobníku s.pop();  // aktualizuje další ukazatel v seznamu temp = temp->next;  } temp->dalsi = NULL; } // Funkce pro zobrazení prvků v seznamu void printlist(Node* temp) { while (temp != NULL) { cout<< temp->data<< ' ';  temp = temp->další;  } } // Program pro vložení zpět do propojeného seznamu void insert_back(Node** head, int value) { // pro zadávání hodnot // do seznamu jsme použili metodu insertion at back.(např.: head->1->2->3->4->Null) Node* temp = new Node(value);  temp->dalsi = NULL;  // If *head = NULL if (*head == NULL) { *head = temp;  vrátit se;  } else { Uzel* posledni_uzel = *hlava;  while (poslední_uzel->další != NULL) poslední_uzel = poslední_uzel->další;  poslední_uzel->dalsi = teplota;  vrátit se;  } } // Kód ovladače int main() { Uzel* head = NULL;  insert_back(&head, 1);  insert_back(&head, 2);  insert_back(&head, 3);  insert_back(&head, 4);  cout<< 'Given linked list
';  printlist(head);  reverseLL(&head);  cout << '
Reversed linked list
';  printlist(head);  return 0; } // This code is contributed by Aditya Kumar (adityakumar129)>
Jáva
// Java program for above approach import java.util.*; class GFG {  // Create a class Node to enter values and address in  // the list  static class Node {  int data;  Node next;  Node(int x) {  data = x;  next = null;  }  };  static Node head = null;  // Function to reverse the linked list  static void reverseLL()  {  // Create a stack 's' of Node type  Stacks = new Stack();  Teplota uzlu = hlava;  while (temp.next != null) { // Vložení všech uzlů do zásobníku s.add(temp);  temp = temp.další;  } hlava = teplota;  while (!s.isEmpty()) { // Uložení nejvyšší hodnoty zásobníku do seznamu temp.next = s.peek();  // Vyber hodnotu ze zásobníku s.pop();  // aktualizuje další ukazatel v seznamu temp = temp.next;  } temp.dalsi = null;  } // Funkce pro zobrazení prvků v seznamu static void printlist(Node temp) { while (temp != null) { System.out.print(temp.data + ' ');  temp = temp.další;  } } // Program pro vložení zpět do propojeného seznamu static void insert_back(int value) { // pro zadání // hodnot v seznamu jsme použili metodu insertion at back. (např.: head.1.2.3.4.Null) Uzel temp = new Node(value);  temp.next = null;  // If *head se rovná null if (head == null) { head = temp;  vrátit se;  } else { Uzel posledni_uzel = hlava;  while (poslední_uzel.další != null) poslední_uzel = poslední_uzel.další;  poslední_uzel.další = teplota;  vrátit se;  } } // Kód ovladače public static void main(String[] args) { insert_back(1);  vložit_zpět(2);  vložit_zpět(3);  vložit_zpět(4);  System.out.print('Zadaný seznam odkazů
');  tiskový seznam(hlava);  reverseLL();  System.out.print('
Obrácený seznam odkazů
');  tiskový seznam(hlava);  } } // Tento kód přidal Aditya Kumar (adityakumar129)>
Krajta
# Python code for the above approach # Definition for singly-linked list. class ListNode: def __init__(self, val = 0, next=None): self.val = val self.next = next class Solution: # Program to reverse the linked list # using stack def reverseLLUsingStack(self, head): # Initialise the variables stack, temp = [], head while temp: stack.append(temp) temp = temp.next head = temp = stack.pop() # Until stack is not # empty while len(stack)>0: temp.next = stack.pop() temp = temp.next temp.next = Žádné return head # Kód ovladače if __name__ == '__main__': head = ListNode(1, ListNode(2, ListNode(3, ListNode(4)))) print('Daný propojený seznam') temp = head while temp: print(temp.val, end=' ') temp = temp.next obj = Solution() print(' 
Obrácený propojený seznam') head = obj.reverseLLUsingStack(head) while head: print(head.val, end=' ') head = head.next>
C#
// C# program for above approach using System; using System.Collections.Generic; class GFG {  // Create a class Node to enter  // values and address in the list  public class Node {  public int data;  public Node next;  public Node(int x) {  data = x;  }  };  static Node head = null;  // Function to reverse the  // linked list  static void reverseLL()  {  // Create a stack 's'  // of Node type  Stacks = nový zásobník();  Teplota uzlu = hlava;  while (temp.next != null) { // Vloží všechny uzly // do zásobníku s.Push(temp);  temp = temp.další;  } hlava = teplota;  while (s.Count != 0) { // Uložení nejvyšší hodnoty // zásobníku do seznamu temp.next = s.Peek();  // Vyskočí hodnotu ze zásobníku s.Pop();  // Aktualizace dalšího ukazatele v // v seznamu temp = temp.next;  } temp.dalsi = null;  } // Funkce pro zobrazení // prvků v List static void printlist(Node temp) { while (temp != null) { Console.Write(temp.data + ' ');  temp = temp.další;  } } // Funkce pro vložení zpět // propojeného seznamu static void insert_back(int val) { // Použili jsme metodu vložení na zadní // k zadání hodnot do seznamu. (např.: // head.1.2.3.4 .Null) Teplota uzlu = new Node(val);  temp.next = null;  // If *head se rovná null if (head == null) { head = temp;  vrátit se;  } else { Uzel posledni_uzel = hlava;  while (poslední_uzel.další != null) { poslední_uzel = poslední_uzel.další;  } poslední_uzel.další = teplota;  vrátit se;  } } // Kód ovladače public static void Main(String[] args) { insert_back(1);  vložit_zpět(2);  vložit_zpět(3);  vložit_zpět(4);  Console.Write('Zadaný seznam odkazů
');  tiskový seznam(hlava);  reverseLL();  Console.Write('
Obrácený seznam odkazů
');  tiskový seznam(hlava);  } } // Tento kód přispěl gauravrajput1>
Javascript
>

Výstup
Given linked list 1 2 3 4 Reversed linked list 4 3 2 1>

Časová náročnost: O(N), návštěva každého uzlu propojeného seznamu velikosti N.
Pomocný prostor: O(N), Prostor se používá k uložení uzlů v zásobníku.