madhu242

Linked list reversal

Oct 25th, 2022 (edited)
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. // In place reversal linked list
  2.  
  3. using namespace std;
  4.  
  5. #include <iostream>
  6.  
  7. class ListNode {
  8.  public:
  9.   int value = 0;
  10.   ListNode *next;
  11.  
  12.   ListNode(int value) {
  13.     this->value = value;
  14.     next = nullptr;
  15.   }
  16. };
  17.  
  18. class ReverseLinkedList {
  19.  public:
  20.   static ListNode *reverse(ListNode *head) {
  21.     if (!head) {
  22.       return head;
  23.     }
  24.     ListNode *current = head;
  25.     ListNode *prevPtr = nullptr;
  26.     ListNode *nextPtr = nullptr;
  27.     while (current) {
  28.       nextPtr = current->next;
  29.       current->next = prevPtr;
  30.       prevPtr = current;
  31.       current = nextPtr;
  32.     }
  33.     return prevPtr;
  34.   }
  35. };
  36.  
  37. int main(int argc, char *argv[]) {
  38.   ListNode *head = new ListNode(2);
  39.   head->next = new ListNode(4);
  40.   head->next->next = new ListNode(6);
  41.   head->next->next->next = new ListNode(8);
  42.   head->next->next->next->next = new ListNode(10);
  43.  
  44.   ListNode *result = ReverseLinkedList::reverse(head);
  45.   cout << "Nodes of the reversed LinkedList are: ";
  46.   while (result != nullptr) {
  47.     cout << result->value << " ";
  48.     result = result->next;
  49.   }
  50. }
  51.  
  52.  
  53. // Reverse sublist of linkedList
  54. using namespace std;
  55.  
  56. #include <iostream>
  57.  
  58. class ListNode {
  59.  public:
  60.   int value = 0;
  61.   ListNode *next;
  62.  
  63.   ListNode(int value) {
  64.     this->value = value;
  65.     next = nullptr;
  66.   }
  67. };
  68.  
  69. class ReverseSubList {
  70.  public:
  71.   static ListNode *reverse(ListNode *head, int p, int q) {
  72.     if (p == q) {
  73.       return head;
  74.     }
  75.  
  76.     // after skipping 'p-1' nodes, current will point to 'p'th node
  77.     ListNode *current = head, *previous = nullptr;
  78.     for (int i = 0; current != nullptr && i < p - 1; ++i) {
  79.       previous = current;
  80.       current = current->next;
  81.     }
  82.  
  83.     // we are interested in three parts of the LinkedList, part before index 'p', part between 'p'
  84.     // and 'q', and the part after index 'q'
  85.     ListNode *lastNodeOfFirstPart = previous;  // points to the node at index 'p-1'
  86.  
  87.     // after reversing the LinkedList 'current' will become the last node of the sub-list
  88.     ListNode *lastNodeOfSubList = current;
  89.     ListNode *next = nullptr;  // will be used to temporarily store the next node
  90.  
  91.     // reverse nodes between 'p' and 'q'
  92.     for (int i = 0; current != nullptr && i < q - p + 1; i++) {
  93.       next = current->next;
  94.       current->next = previous;
  95.       previous = current;
  96.       current = next;
  97.     }
  98.  
  99.     // connect with the first part
  100.     if (lastNodeOfFirstPart != nullptr) {
  101.       lastNodeOfFirstPart->next = previous;  // 'previous' is now the first node of the sub-list
  102.     } else {  // this means p == 1 i.e., we are changing the first node (head) of the LinkedList
  103.       head = previous;
  104.     }
  105.  
  106.     // connect with the last part
  107.     lastNodeOfSubList->next = current;
  108.  
  109.     return head;
  110.   }
  111. };
  112.  
  113. int main(int argc, char *argv[]) {
  114.   ListNode *head = new ListNode(1);
  115.   head->next = new ListNode(2);
  116.   head->next->next = new ListNode(3);
  117.   head->next->next->next = new ListNode(4);
  118.   head->next->next->next->next = new ListNode(5);
  119.  
  120.   ListNode *result = ReverseSubList::reverse(head, 2, 4);
  121.   cout << "Nodes of the reversed LinkedList are: ";
  122.   while (result != nullptr) {
  123.     cout << result->value << " ";
  124.     result = result->next;
  125.   }
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment