jayati

Reverse Linked List

Mar 15th, 2023
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  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 *pt1=head;
  15.       ListNode *pt2=nullptr;
  16.       while(head)
  17.       {
  18.           pt1=head->next;
  19.           head->next=pt2;
  20.           pt2=head;
  21.           head=pt1;
  22.       }
  23.  
  24.     return pt2;
  25.        
  26.     }
  27. };
Advertisement
Add Comment
Please, Sign In to add comment