nikunjsoni

206

Mar 16th, 2021
68
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Definition for singly-linked list.
  3.  * struct ListNode {
  4.  *     int val;
  5.  *     ListNode *next;
  6.  *     ListNode() : val(0), next(nullptr) {}
  7.  *     ListNode(int x) : val(x), next(nullptr) {}
  8.  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
  9.  * };
  10.  */
  11. class Solution {
  12. public:
  13.     ListNode* reverseList(ListNode* head) {
  14.         ListNode *start, *end, *tmp;
  15.         start = end = head;
  16.        
  17.         while(end && end->next) end = end->next;
  18.         while(start != end){
  19.             tmp = start;
  20.             start = start->next;
  21.             tmp->next = end->next;
  22.             end->next = tmp;
  23.         }
  24.         return start;
  25.     }
  26. };
RAW Paste Data