maycod23

Linked_List

Jun 4th, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class node {
  5. public:
  6. int data;
  7. node* next;
  8. node(int data)
  9. {
  10. this->data = data;
  11. next = NULL;
  12. }
  13. };
  14.  
  15. void takeinput(node* &head)
  16. {
  17. int data; cin >> data;
  18. node* temp = NULL;
  19. while (data != -1)
  20. {
  21. node* n1 = new node(data);
  22. if (head == NULL)
  23. {
  24. head = n1; temp = n1;
  25. }
  26. else {
  27. (temp->next) = n1;
  28. temp = (temp->next);
  29. }
  30. cin >> data;
  31. }
  32. }
  33.  
  34. void printlist(node* head) {
  35. while (head->next != NULL) {
  36. cout << head->data << " ";
  37. head = head->next;
  38. }
  39. cout << head->data;
  40. }
  41.  
  42. node* deletenafterm(node* &head, int n, int m)
  43. {
  44. // iterate till m first
  45. int count = 0;
  46. node* temp = head;
  47. while (count != m) {
  48. count++;
  49. if (count == m) break;
  50. temp = temp->next;
  51. }
  52. node* dummy = temp->next;
  53. count = 0;
  54. while (count != n) {
  55. count++;
  56. dummy = dummy->next;
  57. }
  58. temp->next = dummy;
  59. return head;
  60. }
  61.  
  62. void deletemiddle(node* &head) {
  63. //1 2 3 4 5-> 1 2 4 5
  64. //1 2 3 4-> 1 2 4
  65. node* fast = head;
  66. node* slow = head;
  67. node* prev = NULL;
  68. while (fast != NULL && fast->next != NULL) {
  69. prev = slow;
  70. slow = slow->next;
  71. fast = fast->next->next;
  72. }
  73. prev->next = slow->next;
  74. }
  75.  
  76. void deletekthfromend(node* &head, int k)
  77. {
  78. //1 2 3 4 5 6 7 8 -1, k=4
  79. node* p1 = head;
  80. node* p2 = head;
  81. node* prev = NULL;
  82. int count = 0;
  83. while (count != k) {
  84. count++;
  85. p2 = p2->next;
  86. }
  87. while (p2) {
  88. prev = p1;
  89. p1 = p1->next;
  90. p2 = p2->next;
  91. }
  92. if (prev == NULL) {
  93. prev = head->next;
  94. head = prev;
  95. }
  96. else {
  97. prev->next = p1->next;
  98. }
  99. }
  100.  
  101. node* removeelements(node* &head, int val) {
  102. //remove all elements in the linked list with value val without modification and retunr the
  103. //head of a linked list
  104. if (head == NULL) return NULL;
  105. while (head != NULL && head->data == val) {
  106. head = head->next;
  107. }
  108. node* temp = head;
  109. node* prev = NULL;
  110. while (temp != NULL) {
  111. if (temp->data != val) {
  112. prev = temp; temp = temp ->next; continue;
  113. } else {
  114. prev->next = temp->next;
  115. temp = temp->next;
  116. }
  117. }
  118. return head;
  119. }
  120.  
  121.  
  122. node* itreverse(node* head)
  123. {
  124. node* dummy = NULL;
  125. while (head != NULL)
  126. {
  127. node* temp = head->next;
  128. head->next = dummy;
  129. dummy = head;
  130. head = temp;
  131. }
  132. head = dummy;
  133. return head;
  134. }
  135. node* recreverse(node* head)
  136. {
  137. if (head == NULL || head->next == NULL) return head;
  138.  
  139. node* newhead = recreverse(head->next);
  140. node* temp = head->next;
  141. head->next = NULL;
  142. temp->next = head;
  143. return newhead;
  144. }
  145.  
  146. node* cycle(node* & head)
  147. {
  148. node* fast = head;
  149. node* slow = head;
  150. while (fast != NULL && fast->next != NULL)
  151. {
  152. slow = slow->next;
  153. fast = fast->next->next;
  154. if (fast == slow) return slow;
  155. }
  156. return NULL;
  157. }
  158.  
  159. void removecycle(node* head)
  160. {
  161. if (cycle(head))
  162. {
  163. node* temp = head;
  164. node* slow = cycle(head);
  165. node* prev = NULL;
  166. while (temp != slow)
  167. {
  168. prev = slow;
  169. temp = temp->next;
  170. slow = slow->next;
  171. }
  172. }
  173. }
  174. int main()
  175. {
  176. node* head = NULL;
  177. takeinput(head);
  178. // int k; cin >> k;
  179. // kthfromlast(head, k);
  180. // printlist(head); cout << endl;
  181. // head = deletenafterm(head, 3, 5);
  182. // printlist(head);
  183. // 1 2 3 4 5 6 7 8 9 10 -1
  184.  
  185.  
  186. // deletemiddle(head);
  187. // node* head1 = itreverse(head);
  188. // printlist(head1); cout << endl;
  189. node* head2 = recreverse(head);
  190. printlist(head2); cout << endl;
  191.  
  192. return 0;
  193. }
  194.  
  195.  
  196.  
  197. // class Solution {
  198. // public:
  199. // void getmiddle(ListNode*& head,ListNode*& middle,ListNode*& prev)
  200. // {
  201. // ListNode* fast=head;
  202. // ListNode* slow=head;
  203. // while(fast!=NULL&&fast->next!=NULL)
  204. // {
  205. // prev=slow;
  206. // slow=slow->next;
  207. // fast=fast->next->next;
  208. // }
  209. // middle=slow;
  210. // }
  211. // void itreverse(ListNode*& head)
  212. // {
  213. // ListNode* dummy=NULL;
  214. // while(head!=NULL)
  215. // {
  216. // ListNode* next=head->next;
  217. // head->next=dummy;
  218. // dummy=head;
  219. // head=next;
  220. // }
  221. // head=dummy;
  222. // }
  223. // bool isPalindrome(ListNode* head) {
  224. // //a b c d e
  225. // //a b c d e f
  226. // ListNode* middle;
  227. // ListNode* prev;
  228. // getmiddle(head,middle,prev);
  229. // prev->next=NULL;
  230.  
  231. // itreverse(middle);
  232.  
  233. // bool ans=true;
  234. // while(head!=NULL&&middle!=NULL)
  235. // {
  236. // if(head->val!=middle->val){
  237. // ans=false; break;
  238. // }
  239. // head=head->next;
  240. // middle=middle->next;
  241. // }
  242. // return ans;
  243.  
  244. // }
  245. // };
Add Comment
Please, Sign In to add comment