反转链表,用了一个比较笨的方法。

       

public class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null)
            return head;
         ArrayList<Integer> list = new ArrayList<Integer>();
        ListNode p = head;
        while(p!=null) {
            list.add(p.val);
            p = p.next;
        }
        ListNode q = head;
        for(int i=list.size()-1; i>=0; i--) {
            q.val = list.get(i);
            q = q.next;
        }
        return head;
        
    }
}

 

相关文章:

  • 2021-08-20
  • 2021-10-14
  • 2021-10-13
  • 2021-06-22
  • 2021-09-18
  • 2022-01-20
  • 2021-12-16
  • 2021-06-13
猜你喜欢
  • 2021-08-02
  • 2021-06-09
  • 2021-05-27
  • 2021-05-28
  • 2022-12-23
  • 2020-02-14
  • 2019-09-17
相关资源
相似解决方案