I don't know why my code does not work.


  • -1
    A

    /**

    • Definition for singly-linked list.

    • public class ListNode {

    • int val;
      
    • ListNode next;
      
    • ListNode(int x) {
      
    •     val = x;
      
    •     next = null;
      
    • }
      
    • }
      */
      public class Solution {
      public ListNode rotateRight(ListNode head, int n) {

       for (int i=0; i<n ; i++){
           ListNode previous = null;
           ListNode current = head;
           while(current.next != null){
               previous = current;
               current = current.next;
           }
           current.next = head;
           head = current;
           previous.next = null;
       }
       return head;
      

      }
      }

    It says that Runtime Error Message: Line 18: java.lang.NullPointerException, but i don't see the problem.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.