logo

Apverskite susietą sąrašą

Pateikus žymeklį į susieto sąrašo pagrindinį mazgą, užduotis yra pakeisti susietą sąrašą. Turime pakeisti sąrašą, pakeisdami ryšius tarp mazgų.

Pavyzdžiai :



Įvestis : šio susieto sąrašo vadovas
1->2->3->4->NULL
Išvestis : Susietas sąrašas turėtų būti pakeistas į
4->3->2->1->NULL

Įvestis : šio susieto sąrašo vadovas
1->2->3->4->5->NULL
Išvestis : Susietas sąrašas turėtų būti pakeistas į
5->4->3->2->1->NULL

Įvestis : NULL
Išvestis : NULL



Įvestis : 1->NULL
Išvestis : 1->NULL

Rekomenduojama praktika Apverskite susietą sąrašą Išbandykite!

Apverskite susietą sąrašą iteraciniu metodu:

Idėja yra naudoti tris rodykles curr , ankstesnis, ir Kitas sekti mazgus ir atnaujinti atvirkštines nuorodas.

Norėdami išspręsti problemą, atlikite toliau nurodytus veiksmus.



  • Inicijuokite tris rodykles ankstesnė kaip NULL, curr kaip galva , ir Kitas kaip NULL.
  • Pakartokite per susietą sąrašą. Ciklo metu atlikite šiuos veiksmus:
    • Prieš keisdami Kitas apie curr , laikyti Kitas mazgas
      • kitas = curr -> next
    • Dabar atnaujinkite Kitas rodyklė curr prie ankstesnė
      • curr -> next = ankstesnis
    • Atnaujinti ankstesnė kaip curr ir curr kaip Kitas
      • ankstesnis = Curr
      • curr = kitas

Žemiau pateikiamas pirmiau minėto metodo įgyvendinimas:

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->duomenys = duomenys;  kitas = NULL;  } }; struct LinkedList { Mazgas* head;  LinkedList() { head = NULL; } /* Funkcija pakeisti susietą sąrašą */ void reverse() { // Inicijuoti esamą, ankstesnę ir kitą rodyklę Mazgas* srovė = galva;  Mazgas *ankstesnis = NULL, *kitas = NULL;  while (current != NULL) { // Store next next = dabartinis->kitas;  // Atvirkštinės srovės mazgo rodyklės srovė->kitas = ankstesnis;  // Perkelti žymeklius viena pozicija į priekį.  ankstesnis = srovė;  srovė = kitas;  } galva = ankstesnis;  } /* Funkcija spausdinti susietą sąrašą */ void print() { struct Mazgas* temp = head;  while (temp != NULL) { cout<< temp->duomenis<< ' ';  temp = temp->Kitas;  } } void push(int data) { Mazgas* temp = naujas Mazgas(duomenys);  temp->kitas = galva;  galva = temp;  } }; /* Tvarkyklės kodas*/ int main() { /* Pradėkite nuo tuščio sąrašo */ 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->Kitas;  // Atvirkštinės srovės mazgo rodyklės srovė->kitas = ankstesnis;  // Perkelti žymeklius viena pozicija į priekį.  ankstesnis = srovė;  srovė = kitas;  } *head_ref = ankstesnis; } /* Funkcija stumti mazgą */ void push(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));  naujas_mazgas->duomenys = nauji_duomenys;  naujas_mazgas->kitas = (*head_ref);  (*head_ref) = naujas_mazgas; } /* Funkcija spausdinti susietą sąrašą */ void printList(struct Node* head) { struct Node* temp = head;  while (temp != NULL) { printf ('%d ', temp-> data);  temp = temp->kitas;  } } /* Tvarkyklės kodas*/ int main() { /* Pradėkite nuo tuščio sąrašo */ struct Node* head = NULL;  stumti(&galva, 20);  stumti(&galva, 4);  stumti(&galva, 15);  stumti(&galva, 85);  printf('Duotas susietas sąrašas
');  printList(head);  reverse(&head);  printf('
Atvirkštinis susietų sąrašas 
');  printList(head);  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
>>  
Išvestis Laiko sudėtingumas: O(N), einama per susietą N dydžio sąrašą.
Pagalbinė erdvė: O(1)

Apverskite susietą sąrašą naudodami Recursion:

Idėja yra pasiekti paskutinį susieto sąrašo mazgą naudojant rekursiją, tada pradėti apversti susietą sąrašą.

10 ml iki uncijos

Norėdami išspręsti problemą, atlikite toliau nurodytus veiksmus.

  • Padalinkite sąrašą į dvi dalis – pirmąjį mazgą ir likusią susieto sąrašo dalį.
  • Norėdami gauti likusį susietą sąrašą, skambinkite atvirkščiai.
  • Susiekite likusį susietų sąrašą su pirmuoju.
  • Pataisykite galvos žymeklį į NULL

Žemiau pateikiamas aukščiau aprašyto metodo įgyvendinimas:

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->duomenys = duomenys;  kitas = NULL;  } }; struct LinkedList { Mazgas* head;  LinkedList() { head = NULL; } Mazgas* reverse(Node* head) /* Funkcija spausdinti susietą sąrašą */ void print() { struct Mazgas* temp = head;  while (temp != NULL) { cout<< temp->duomenis<< ' ';  temp = temp->Kitas;  } } void push(int data) { Mazgas* temp = naujas Mazgas(duomenys);  temp->kitas = galva;  galva = temp;  } }; /* Tvarkyklės programa, skirta patikrinti aukščiau pateiktą funkciją*/ int main() { /* Pradėkite nuo tuščio sąrašo */ 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
>>  
Išvestis Laiko sudėtingumas: O (N), apsilankymas kiekviename mazge vieną kartą
Pagalbinė erdvė: O(N), funkcijų iškvietimo kamino erdvė

Apverskite susietą sąrašą tail rekursyviniu metodu:

Idėja yra išlaikyti tris nuorodas ankstesnis , srovė ir Kitas , rekursyviai apsilankykite kiekviename mazge ir sukurkite nuorodas naudodami šias tris nuorodas.

Norėdami išspręsti problemą, atlikite toliau nurodytus veiksmus.

  • Pirmas atnaujinimas kitas su kitu srovės mazgu, t.y. kitas = dabartinis->kitas
  • Dabar sukurkite atvirkštinę nuorodą iš dabartinio mazgo į ankstesnį mazgą, t. y. curr->ext = ankstesnis
  • Jei aplankytas mazgas yra paskutinis mazgas, tiesiog sukurkite atvirkštinę nuorodą iš dabartinio mazgo į ankstesnį mazgą ir atnaujinkite galvutę.

Žemiau pateikiamas pirmiau minėto metodo įgyvendinimas:

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->kitas) { *head = curr;  /* Atnaujinimas šalia ankstesnio mazgo */ curr->next = ankstesnis;  grąžinti;  } /* Išsaugoti curr->next node rekursiniam skambučiui */ Mazgas* next = curr->next;  /* ir atnaujinti kitą ..*/ curr->next = ankstesnis;  reverseUtil(kitas, curr, head); } // Naudingumo funkcija, skirta spausdinti susietą sąrašą void printlist(Node* head) { while (head != NULL) { cout<< head->duomenis<< ' ';  head = head->Kitas;  } cout<< endl; } // Driver code int main() {  Node* head1 = new Node(1);  head1->kitas = naujas mazgas (2);  head1->kitas->kitas = naujas mazgas (3);  head1->kitas->kitas->kitas = naujas mazgas (4);  head1->kitas->kitas->kitas->kitas = naujas mazgas(5);  head1->kitas->kitas->kitas->kitas->kitas = naujas mazgas (6);  head1->kitas->kitas->kitas->kitas->kitas->kitas = naujas mazgas (7);  antraštė1->kitas->kitas->kitas->kitas->kitas->kitas->kitas = naujas mazgas(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->kitas) { *head = curr;  /* Atnaujinimas šalia ankstesnio mazgo */ curr->next = ankstesnis;  grąžinti;  } /* Išsaugoti curr->next node rekursiniam skambučiui */ Mazgas* next = curr->next;  /* ir atnaujinti kitą ..*/ curr->next = ankstesnis;  reverseUtil(kitas, curr, head); } // Naudingumo funkcija, skirta sukurti naują mazgą Node* newNode(int key) { Node* temp = (Node*)malloc(sizeof(Node));  temp->duomenys = raktas;  temp->kitas = NULL;  grąžinimo temperatūra; } // Naudingumo funkcija, skirta spausdinti susietą sąrašą void printlist(Node* head) { while (head != NULL) { printf('%d ', head->data);  galva = galva->kita;  } printf('
'); } // Tvarkyklės kodas int main() { Mazgas* head1 = naujasMazgas(1);  head1->ext = naujasMazgas(2);  head1->kitas->kitas = naujasMazgas(3);  head1->kitas->kitas->kitas = naujasMazgas(4);  head1->kitas->kitas->kitas->kitas = naujasMazgas(5);  head1->kitas->kitas->kitas->kitas->kitas = naujasMazgas(6);  antraštė1->kitas->kitas->kitas->kitas->kitas->kitas = naujasMazgas(7);  antraštė1->kitas->kitas->kitas->kitas->kitas->kitas->kitas = naujasMazgas(8);  printf('Duotas susietas sąrašas
');  spausdinimo sąrašas(head1);  reverse(&head1);  printf('Atvirkštinis susietų sąrašas
');  spausdinimo sąrašas(head1);  grąžinti 0; } // Šį kodą sukūrė Aditya Kumar (adityakumar129)>>Java>Python
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->kitas mazgas rekursiniam skambučiui */ Mazgas next1 = curr.next;  /* ir atnaujinti kitą ..*/ curr.next = ankstesnis;  reverseUtil(next1, curr);  grąžinimo galvutė;  } // spausdina dvigubai susieto sąrašo turinį void printList(Node node) { while (mazgas != null) { Console.Write(mazgas.duomenys + ' ');  mazgas = mazgas.kitas;  } } // Tvarkyklės kodas public static void Main(String[] args) { LinkedList list = new LinkedList();  list.head = naujas mazgas(1);  list.head.ext = naujas mazgas(2);  list.head.ext.ext = naujas mazgas(3);  list.head.ext.ext.next = naujas mazgas(4);  list.head.next.ext.next.next = naujas mazgas(5);  list.head.next.ext.ext.next.next = naujas mazgas(6);  list.head.next.ext.ext.next.next.next = naujas mazgas(7);  list.head.next.ext.ext.next.next.next.next = naujas Mazgas(8);  Console.WriteLine('Duotas susietas sąrašas ');  list.printList(list.head);  Mazgas res = list.reverseUtil(list.head, null);  Console.WriteLine('
Atvirkštinis susietų sąrašas ');  list.printList(res);  } } // Šį kodą pateikė Rajput-Ji>
Javascript
>>  
Išvestis Laiko sudėtingumas: O (N), lankantis kiekviename susieto N dydžio sąrašo mazge.
Pagalbinė erdvė: O(N), funkcijų iškvietimo kamino erdvė

Apverskite susietą sąrašą naudodami Idėja yra saugoti visus mazgus krūvoje, tada sudaryti atvirkštinio susiejimo sąrašą.

Norėdami išspręsti problemą, atlikite toliau nurodytus veiksmus.

  • Išsaugokite mazgus (reikšmes ir adresą) krūvoje, kol bus įvestos visos reikšmės.
  • Atlikę visus įrašus, atnaujinkite galvos žymeklį į paskutinę vietą (ty paskutinę reikšmę).
  • Pradėkite iškelti mazgus (vertę ir adresą) ir saugokite juos ta pačia tvarka, kol krūva bus tuščia.
  • Atnaujinkite kitą paskutinio mazgo žymeklį krūvoje NULL.

Žemiau pateikiamas pirmiau minėto metodo įgyvendinimas:

tostring metodas java
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;  Mazgas* temp = *galva;  while (temp->next != NULL) { // Įstumkite visus mazgus į krūvą s.push(temp);  temp = temp->kitas;  } *galva = temp;  while (!s.empty()) { // Išsaugokite viršutinę krūvos reikšmę sąraše temp->next = s.top();  // Iškelti reikšmę iš kamino s.pop();  // atnaujinkite kitą žymeklį sąraše temp = temp->next;  } temp->kitas = NULL; } // Funkcija rodyti elementus sąraše void printlist(Node* temp) { while (temp != NULL) { cout<< temp->duomenis<< ' ';  temp = temp->Kitas;  } } // Programa, skirta įterpti atgal į susietą sąrašą void insert_back(Node** head, int value) { // mes naudojome įterpimo atgal metodą, norėdami įvesti reikšmes // sąraše.(pvz: head->1->2->3->4->Null) Mazgas* temp = naujas Mazgas(reikšmė);  temp->kitas = NULL;  // Jei *head lygi NULL if (*head == NULL) { *head = temp;  grąžinti;  } else { Mazgas* paskutinis_mazgas = *galva;  while (paskutinis_mazgas->kitas != NULL) paskutinis_mazgas = paskutinis_mazgas->kitas;  paskutinis_mazgas->kitas = temp;  grąžinti;  } } // Tvarkyklės kodas int main() { Mazgas* 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 = naujas Stack();  Mazgo temp = galva;  while (temp.next != null) { // Įstumkite visus mazgus į krūvą s.add(temp);  temp = temp.kitas;  } galva = temp;  while (!s.isEmpty()) { // Išsaugokite aukščiausią krūvos reikšmę sąraše temp.next = s.peek();  // Iškelti reikšmę iš kamino s.pop();  // atnaujinkite kitą sąrašo rodyklę temp = temp.next;  } temp.next = null;  } // Funkcija rodyti elementus sąraše static void printlist(Node temp) { while (temp != null) { System.out.print(temp.data + ' ');  temp = temp.kitas;  } } // Programa, skirta įterpti atgal į susietą sąrašą static void insert_back(int value) { // naudojome įterpimo atgal metodą, kad įvestume // reikšmes sąraše.(pvz.: head.1.2.3.4.Null) Mazgas temp = naujas Mazgas(reikšmė);  temp.next = null;  // Jei *head lygi nuliui if (head == null) { head = temp;  grąžinti;  } else { Mazgas paskutinis_mazgas = galva;  while (paskutinis_mazgas.next != null) paskutinis_mazgas = paskutinis_mazgas.next;  paskutinis_mazgas.next = temp;  grąžinti;  } } // Tvarkyklės kodas public static void main(String[] args) { insert_back(1);  įterpti_atgal(2);  įterpti_atgal(3);  įterpti_atgal(4);  System.out.print('Duotas susietas sąrašas
');  spausdinimo sąrašas (galva);  reverseLL();  System.out.print('
Atvirkštinis susietų sąrašas
');  spausdinimo sąrašas (galva);  } } // Šį kodą sukūrė Aditya Kumar (adityakumar129)>>Python
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 = naujas krūvas();  Mazgo temp = galva;  while (temp.next != null) { // Įstumkite visus mazgus // į krūvą s.Push(temp);  temp = temp.kitas;  } galva = temp;  while (s.Count != 0) { // Išsaugokite aukščiausią // krūvos reikšmę sąraše temp.next = s.Peek();  // Iškelti reikšmę iš kamino s.Pop();  // Atnaujinkite kitą rodyklę sąraše // temp = temp.next;  } temp.next = null;  } // Funkcija rodyti // elementų sąraše static void printlist(Node temp) { while (temp != null) { Console.Write(temp.data + ' ');  temp = temp.kitas;  } } // Funkcija įterpti atgal į susietą sąrašą // static void insert_back(int val) { // Mes naudojome įterpimo atgal metodą // norėdami įvesti reikšmes į sąrašą.(pvz.: // head.1.2.3.4 .Null) Mazgo temp = new Node(val);  temp.next = null;  // Jei *head lygi nuliui if (head == null) { head = temp;  grąžinti;  } else { Mazgas paskutinis_mazgas = galva;  while (paskutinis_mazgas.next != null) { paskutinis_mazgas = paskutinis_mazgas.next;  } paskutinis_mazgas.next = temp;  grąžinti;  } } // Vairuotojo kodas public static void Main(String[] args) { insert_back(1);  įterpti_atgal(2);  įterpti_atgal(3);  įterpti_atgal(4);  Console.Write('Duotas susietas sąrašas
');  spausdinimo sąrašas (galva);  reverseLL();  Console.Write('
Atvirkštinis susietų sąrašas
');  spausdinimo sąrašas (galva);  } } // Šį kodą pateikė gauravrajput1>
Javascript
>>  
Išvestis Laiko sudėtingumas: O (N), lankantis kiekviename susieto N dydžio sąrašo mazge.
Pagalbinė erdvė: O(N), tarpas naudojamas mazgams dėtuvėje saugoti.