/*
public class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;}
}*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Solution {public ListNode ReverseList(ListNode head) {if(head==null){return null;}List<ListNode> list = new ArrayList<>();while(head!=null){list.add(head);head=head.next;}Collections.reverse(list);//翻轉鏈表///例如翻轉后的鏈表為:5 4 3 2 1//需要根據list創建單鏈表head=list.get(0);//頭節點的值設定for(int i=list.size()-1;i>=1;i--){ListNode p = new ListNode(list.get(i).val);//利用頭插法創建單鏈表p.next=head.next;head.next=p;}return head;}
}
public class Solution {public ListNode ReverseList(ListNode head) {if(head==null){return null;}ListNode preNode=null;ListNode nextNode=null; while (head!=null){nextNode=head.next;head.next=preNode;preNode=head;head=nextNode;}return preNode;}
}