/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode reverseList(ListNode head) { if(head == null)return head; ListNode cur1 = null; ListNode cur2 = head; while(cur2.next != null){ ListNode temp = cur2.next; cur2.next = cur1; cur1 = cur2; cur2 = temp; } cur2.next = cur1; return cur2; } }