gelita

Reverse a linked list

Feb 27th, 2020
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.37 KB | None | 0 0
  1. class Solution {
  2.     public ListNode reverseList(ListNode head) {
  3.         ListNode prev = null;
  4.         ListNode curr = head;
  5.         ListNode next = null;
  6.        
  7.         while(curr != null){
  8.             next = curr.next;
  9.             curr.next = prev;
  10.             prev = curr;
  11.             curr = next;
  12.         }
  13.         head = prev;
  14.         return head;
  15.     }
  16. }
Advertisement
Add Comment
Please, Sign In to add comment