Why do I always show time is too long?


  • -2
    W

    /**

    • Definition for singly-linked list.

    • public class ListNode {

    • int val;
      
    • ListNode next;
      
    • ListNode(int x) { val = x; }
      
    • }
      */
      public class Solution {
      public ListNode reverseList(ListNode head)
      {
      if(head==null||head.next==null)
      return head;
      ListNode p=head.next;

        while(p!=null)
        {
             ListNode temp=p;   
             temp.next=head.next;
             head.next=temp;
             p=p.next;
        }
        return head;
      

      }


Log in to reply
 

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