Reverse a singly linked list.
这道题非常基础,需要熟练掌握
/** * 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) { ListNode pre = null; while(head != null){ ListNode node = new ListNode(head.val); node.next = pre; pre = node; head = head.next; } return pre; } }