logo

Saistītā saraksta apvēršana

Ja ir norādīts rādītājs uz saistītā saraksta galveno mezglu, uzdevums ir apgriezt saistīto sarakstu. Mums ir jāapgriež saraksts, mainot saites starp mezgliem.

Piemēri :



Ievade : šāda saistītā saraksta vadītājs
1->2->3->4->NULL
Izvade : Saistītais saraksts ir jāmaina uz,
4->3->2->1->NULL

Ievade : šāda saistītā saraksta vadītājs
1->2->3->4->5->NULL
Izvade : Saistītais saraksts ir jāmaina uz,
5->4->3->2->1->NULL

Ievade : NULL
Izvade : NULL



Ievade : 1->NULL
Izvade : 1->NULL

apakšvirkne Java
Ieteicamā prakse Apgriezt saistīto sarakstu Izmēģiniet to!

Saistītā saraksta apvēršana, izmantojot iteratīvo metodi:

Ideja ir izmantot trīs norādes curr , iepriekšējā, un Nākamais lai izsekotu mezgliem, lai atjauninātu apgrieztās saites.

Lai atrisinātu problēmu, veiciet tālāk norādītās darbības.



  • Inicializējiet trīs norādes iepriekj kā NULL, curr galvu , un Nākamais kā NULL.
  • Atkārtojiet, izmantojot saistīto sarakstu. Ciklā veiciet tālāk norādītās darbības.
    • Pirms mainīt Nākamais no curr , uzglabājiet Nākamais mezgls
      • nākamais = curr -> next
    • Tagad atjauniniet Nākamais rādītājs no curr uz iepriekj
      • curr -> next = iepriekj
    • Atjaunināt iepriekj curr un curr Nākamais
      • iepriekj = curr
      • curr = nākamais

Tālāk ir aprakstīta iepriekš minētās pieejas īstenošana.

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->dati = dati;  nākamais = NULL;  } }; struct LinkedList { Node* head;  LinkedList() { head = NULL; } /* Funkcija, lai apgrieztu saistīto sarakstu */ void reverse() { // Inicializēt pašreizējo, iepriekšējo un nākamo rādītāju Mezgls* pašreizējais = galva;  Mezgls *iepriekšējais = NULL, *nākamais = NULL;  while (current != NULL) { // Saglabāt nākamo nākamo = pašreizējais->nākamais;  // Reversās strāvas mezgla rādītāja strāva->nākamais = iepriekšējais;  // Pārvietojiet rādītājus vienu pozīciju uz priekšu.  prev = strāva;  pašreizējais = nākamais;  } galva = iepriekj;  } /* Saistītā saraksta drukāšanas funkcija */ void print() { struct Node* temp = head;  while (temp != NULL) { cout<< temp->datus<< ' ';  temp = temp->Nākamais;  } } void push(int data) { Node* temp = new Node(data);  temp->nākamais = galva;  galva = temp;  } }; /* Draivera kods*/ int main() { /* Sāciet ar tukšo sarakstu */ 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->Nākamais;  // Reversās strāvas mezgla rādītāja strāva->nākamais = iepriekšējais;  // Pārvietojiet rādītājus vienu pozīciju uz priekšu.  prev = strāva;  pašreizējais = nākamais;  } *head_ref = iepriekšējais; } /* Funkcija, lai nospiestu mezglu */ void push(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));  jauns_mezgls->dati = jauni_dati;  jauns_mezgls->nākamais = (*head_ref);  (*head_ref) = jauns_mezgls; } /* Saistītā saraksta drukāšanas funkcija */ void printList(struct Node* head) { struct Node* temp = head;  while (temp != NULL) { printf ('%d ', temp-> data);  temp = temp->nākamais;  } } /* Draivera kods*/ int main() { /* Sāciet ar tukšo sarakstu */ struct Node* head = NULL;  push(&galva, 20);  push(&galva, 4);  push(&galva, 15);  push(&galva, 85);  printf('Dotais saistītais saraksts
');  drukas saraksts(galva);  reverse(&head);  printf('
Apgrieztais saistītais saraksts 
');  drukas saraksts(galva);  getchar (); }>
Java
// 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>
Python
# 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
>>  
Izvade Laika sarežģītība: O(N), šķērsojot saistīto N izmēra sarakstu.
Palīgtelpa: O(1)

Saistītā saraksta apvēršana, izmantojot Recursion:

Ideja ir sasniegt saistītā saraksta pēdējo mezglu, izmantojot rekursiju, un pēc tam sākt saistītā saraksta apvēršanu.

Lai atrisinātu problēmu, veiciet tālāk norādītās darbības.

java uzlabota cilpa
  • Sadaliet sarakstu divās daļās – pirmais mezgls un pārējā saistītā saraksta daļa.
  • Lai iegūtu pārējo saistīto sarakstu, zvaniet reverse.
  • Saistiet pārējo saistīto sarakstu ar pirmo.
  • Nostipriniet galvas rādītāju uz NULL

Tālāk ir aprakstīta iepriekš minētās pieejas īstenošana:

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->dati = dati;  nākamais = NULL;  } }; struct LinkedList { Node* head;  LinkedList() { head = NULL; } Node* reverse(Node* head) /* Funkcija, lai drukātu saistīto sarakstu */ void print() { struct Node* temp = head;  while (temp != NULL) { cout<< temp->datus<< ' ';  temp = temp->Nākamais;  } } void push(int data) { Node* temp = new Node(data);  temp->nākamais = galva;  galva = temp;  } }; /* Draivera programma, lai pārbaudītu iepriekš minēto funkciju*/ int main() { /* Sāciet ar tukšo sarakstu */ 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; }>
Java
// 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>
Python
'''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
>>  
Izvade Laika sarežģītība: O(N), apmeklējot katru mezglu vienu reizi
Palīgtelpa: O(N), funkciju izsaukuma steka vieta

Saistītā saraksta apvēršana, izmantojot astes rekursīvo metodi:

Ideja ir saglabāt trīs norādes iepriekšējā , strāva un Nākamais , rekursīvi apmeklējiet katru mezglu un izveidojiet saites, izmantojot šos trīs rādītājus.

pārdēvējiet mapi Linux

Lai atrisinātu problēmu, veiciet tālāk norādītās darbības.

  • Nākamais pirmais atjauninājums ar nākamo strāvas mezglu, t.i. nākamais = pašreizējais->nākamais
  • Tagad izveidojiet apgriezto saiti no pašreizējā mezgla uz iepriekšējo mezglu, t.i. curr->next = prev
  • Ja apmeklētais mezgls ir pēdējais mezgls, vienkārši izveidojiet apgriezto saiti no pašreizējā mezgla uz iepriekšējo mezglu un atjauniniet galvu.

Tālāk ir aprakstīta iepriekš minētās pieejas īstenošana.

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->nākamais) { *head = curr;  /* Atjaunināt blakus iepriekšējam mezglam */ curr->next = prev;  atgriešanās;  } /* Saglabāt curr->next node for recursive call */ Node* next = curr->next;  /* un atjaunina nākamo ..*/ curr->next = iepriekšējā;  reverseUtil(nākamais, curr, head); } // Lietderības funkcija saistītā saraksta drukāšanai void printlist(Node* head) { while (head != NULL) { cout<< head->datus<< ' ';  head = head->Nākamais;  } cout<< endl; } // Driver code int main() {  Node* head1 = new Node(1);  head1->nākamais = jauns mezgls(2);  head1->nākamais->nākamais = jauns mezgls(3);  head1->nākamais->nākamais->nākamais = jauns mezgls(4);  head1->nākamais->nākamais->nākamais->nākamais = jauns mezgls(5);  head1->nākamais->nākamais->nākamais->nākamais->nākamais = jauns mezgls(6);  head1->nākamais->nākamais->nākamais->nākamais->nākamais->nākamais = jauns mezgls(7);  head1->nākamais->nākamais->nākamais->nākamais->nākamais->nākamais->nākamais = jauns mezgls(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->nākamais) { *head = curr;  /* Atjaunināt blakus iepriekšējam mezglam */ curr->next = prev;  atgriešanās;  } /* Saglabāt curr->next node for recursive call */ Node* next = curr->next;  /* un atjaunināt nākamo ..*/ curr->next = iepriek.  reverseUtil(nākamais, curr, head); } // Lietderības funkcija jauna mezgla izveidei Node* newNode(int key) { Node* temp = (Node*)malloc(sizeof(Node));  temp->data = atslēga;  temp->nākamais = NULL;  atgriešanās temperatūra; } // Lietderības funkcija saistītā saraksta drukāšanai void printlist(Node* head) { while (head != NULL) { printf('%d ', head->data);  galva = galva->nākamais;  } printf('
'); } // Draivera kods int main() { Node* head1 = newNode(1);  head1->next = jaunsMezgls(2);  head1->nākamais->nākamais = jaunsMezgls(3);  head1->nākamais->nākamais->nākamais = newNode(4);  head1->nākamais->nākamais->nākamais->nākamais = newNode(5);  head1->nākamais->nākamais->nākamais->nākamais->nākamais = newNode(6);  head1->nākamais->nākamais->nākamais->nākamais->nākamais->nākamais = newNode(7);  head1->nākamais->nākamais->nākamais->nākamais->nākamais->nākamais->nākamais = newNode(8);  printf('Dotais saistītais saraksts
');  drukas saraksts(head1);  reverse(&head1);  printf('Apgrieztais saistītais saraksts
');  drukas saraksts(head1);  atgriezties 0; } // Šo kodu ir sagatavojusi Aditya Kumar (adityakumar129)>
Java
// 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->nākamais mezgls rekursīvajam izsaukumam */ Node next1 = curr.next;  /* un atjaunināt nākamo ..*/ curr.next = iepriek.  reverseUtil(next1, curr);  atgriešanās galva;  } // izdrukā dubultsaites saraksta saturu void printList(Node node) { while (node ​​!= null) { System.out.print(node.data + ' ');  mezgls = mezgls.next;  } } // Draivera kods public static void main(String[] args) { LinkedList list = new LinkedList();  list.head = jauns mezgls(1);  list.head.next = jauns mezgls(2);  saraksts.galva.next.next = jauns mezgls(3);  saraksts.galva.next.next.next = jauns mezgls(4);  saraksts.galva.next.next.next.next = jauns mezgls(5);  saraksts.galva.next.next.next.next.next = jauns mezgls(6);  list.head.next.next.next.next.next.next = jauns mezgls(7);  saraksts.galva.next.next.next.next.next.next.next = jauns mezgls(8);  System.out.println('Dotais saistītais saraksts ');  saraksts.printSaraksts(galva);  Mezgls res = saraksts.reverseUtil(head, null);  System.out.println('
Apgrieztais saistītais saraksts ');  saraksts.printList(res);  } } // Šo kodu ir sagatavojusi Aditya Kumar (adityakumar129)>
Python
# 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->nākamais mezgls rekursīvajam izsaukumam */ Node next1 = curr.next;  /* un atjauniniet nākamo ..*/ curr.next = iepriekj;  reverseUtil(next1, curr);  atgriešanās galva;  } // izdrukā dubultsaites saraksta saturu void printList(Node node) { while (node ​​!= null) { Console.Write(node.data + ' ');  mezgls = mezgls.next;  } } // Draivera kods public static void Main(String[] args) { LinkedList list = new LinkedList();  list.head = jauns mezgls(1);  list.head.next = jauns mezgls(2);  list.head.next.next = jauns mezgls(3);  saraksts.galva.next.next.next = jauns mezgls(4);  saraksts.galva.next.next.next.next = jauns mezgls(5);  saraksts.galva.next.next.next.next.next = jauns mezgls(6);  list.head.next.next.next.next.next.next = jauns mezgls(7);  saraksts.galva.next.next.next.next.next.next.next = jauns mezgls(8);  Console.WriteLine('Dotais saistītais saraksts ');  saraksts.printList(saraksts.galva);  Mezgls res = list.reverseUtil(saraksts.head, null);  Console.WriteLine('
Apgrieztais saistītais saraksts ');  saraksts.printList(res);  } } // Šo kodu sniedz Rajput-Ji>
Javascript
>>  
Izvade Laika sarežģītība: O(N), apmeklējot katru N izmēra saistītā saraksta mezglu.
Palīgtelpa: O(N), funkciju izsaukuma steka vieta

Apgriezt saistīto sarakstu, izmantojot Ideja ir saglabāt visus mezglus kaudzē un pēc tam izveidot apgriezti saistīto sarakstu.

Lai atrisinātu problēmu, veiciet tālāk norādītās darbības.

  • Saglabājiet mezglus (vērtības un adresi) kaudzē, līdz tiek ievadītas visas vērtības.
  • Kad visi ieraksti ir izdarīti, atjauniniet galvas rādītāju uz pēdējo atrašanās vietu (t.i., pēdējo vērtību).
  • Sāciet popping mezglus (vērtību un adresi) un saglabājiet tos tādā pašā secībā, līdz kaudze ir tukša.
  • Atjauniniet nākamo pēdējā mezgla rādītāju kaudzē ar NULL.

Tālāk ir aprakstīta iepriekš minētās pieejas īstenošana.

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;  Mezgls* temp = *galva;  while (temp->next != NULL) { // Ievietojiet visus mezglus, lai steku s.push(temp);  temp = temp->nākamais;  } *galva = temp;  while (!s.empty()) { // Saglabā steka augšējo vērtību sarakstā temp->next = s.top();  // Pārvietot vērtību no steka s.pop();  // atjaunināt nākamo rādītāju sarakstā temp = temp->next;  } temp->nākamais = NULL; } // Funkcija elementu parādīšanai sarakstā Void printlist(Node* temp) { while (temp != NULL) { cout<< temp->datus<< ' ';  temp = temp->Nākamais;  } } // Programma saistītā saraksta aizmugurē ievietošanai void insert_back(Node** head, int value) { // mēs esam izmantojuši metodi insertion at back, lai sarakstā ievadītu vērtības //.(piem.: head->1->2->3->4->Null) Mezgls* temp = jauns Mezgls(vērtība);  temp->nākamais = NULL;  // Ja *head ir vienāds ar NULL if (*head == NULL) { *head = temp;  atgriešanās;  } else { Mezgls* pēdējais_mezgls = *galva;  while (pēdējais_mezgls->nākamais != NULL) pēdējais_mezgls = pēdējais_mezgls->nākamais;  pēdējais_mezgls->nākamais = temp;  atgriešanās;  } } // Draivera kods int main() { Node* 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)>
Java
// 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 = jauns Stack();  Mezgla temp = galva;  while (temp.next != null) { // Ievietojiet visus mezglus steksā s.add(temp);  temp = temp.next;  } galva = temp;  while (!s.isEmpty()) { // Saglabā steka augšējo vērtību sarakstā temp.next = s.peek();  // Pārvietot vērtību no steka s.pop();  // atjaunināt nākamo rādītāju sarakstā temp = temp.next;  } temp.next = null;  } // Funkcija, lai parādītu elementus sarakstā Static void printlist(Node temp) { while (temp != null) { System.out.print(temp.data + ' ');  temp = temp.next;  } } // Programma, lai ievietotu saistītā saraksta aizmugurē static void insert_back(int value) { // esam izmantojuši metodi insertion at back, lai sarakstā ievadītu // vērtības.(piem.: head.1.2.3.4.Null) Node temp = jauns Mezgls(vērtība);  temp.next = null;  // Ja *head ir vienāds ar nulli if (head == null) { head = temp;  atgriešanās;  } else { Mezgls pēdējais_mezgls = galva;  while (pēdējais_mezgls.next != null) pēdējais_node = pēdējais_mezgls.next;  pēdējais_mezgls.next = temp;  atgriešanās;  } } // Draivera kods public static void main(String[] args) { insert_back(1);  ievietot_atpakaļ(2);  ievietot_atpakaļ(3);  ievietot_atpakaļ(4);  System.out.print('Dotais saistītais saraksts
');  drukas saraksts(galva);  reverseLL();  System.out.print('
Apgrieztais saistītais saraksts
');  drukas saraksts(galva);  } } // Šo kodu ir sagatavojusi Aditya Kumar (adityakumar129)>
Python
# 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 = kaudze.pop() temp = temp.next temp.next = Nav atgriež galvu # Draivera kods if __name__ == '__main__': head = ListNode(1, ListNode(2, ListNode(3, ListNode(4)))) print('Given linked list') temp = head while temp: print(temp.val, end=' ') temp = temp.next obj = Solution() print(' 
Apgrieztais saistītais saraksts') 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 = jauns kaudze();  Mezgla temp = galva;  while (temp.next != null) { // Iespiediet visus mezglus //, lai sakrautu s.Push(temp);  temp = temp.next;  } galva = temp;  while (s.Count != 0) { // Saglabā // steka augšējo vērtību sarakstā temp.next = s.Peek();  // Pārvietot vērtību no steka s.Pop();  // Atjaunināt nākamo rādītāju sarakstā // temp = temp.next;  } temp.next = null;  } // Displeja funkcija // elementi sarakstā Static void printlist(Node temp) { while (temp != null) { Console.Write(temp.data + ' ');  temp = temp.next;  } } // Funkcija ievietot // saistītā saraksta aizmugurē static void insert_back(int val) { // Mēs esam izmantojuši metodi insertion at back //, lai sarakstā ievadītu vērtības.(piem.: // head.1.2.3.4 .Null) Node temp = new Node(val);  temp.next = null;  // Ja *head ir vienāds ar nulli if (head == null) { head = temp;  atgriešanās;  } else { Mezgls pēdējais_mezgls = galva;  while (pēdējais_mezgls.next != null) { pēdējais_mezgls = pēdējais_mezgls.next;  } pēdējais_mezgls.next = temp;  atgriešanās;  } } // Draivera kods public static void Main(String[] args) { insert_back(1);  ievietot_atpakaļ(2);  ievietot_atpakaļ(3);  ievietot_atpakaļ(4);  Console.Write('Dotais saistītais saraksts
');  drukas saraksts(galva);  reverseLL();  Console.Write('
Apgrieztais saistītais saraksts
');  drukas saraksts(galva);  } } // Šo kodu ir nodrošinājis gauravrajput1>
Javascript
>>  
Izvade Laika sarežģītība: O(N), apmeklējot katru N izmēra saistītā saraksta mezglu.
Palīgtelpa: O(N), atstarpi izmanto, lai glabātu stekā esošos mezglus.