import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 * @return ListNode类 */ public ListNode reverseList (ListNode head) { ListNode pre = null, post = head, tmp; while (post != null) { tmp = post.next; post.next = pre; pre = post; post = tmp; } return pre; } }
public class Solution { /** * * @param head ListNode类 * @return ListNode类 */ public ListNode reverseList (ListNode head) { // write code here ListNode dummyHead = new ListNode(0); ListNode tail = null; dummyHead.next = tail; // 采用头插法解决此问题 while(head != null){ ListNode node = new ListNode(head.val); node.next = dummyHead.next; dummyHead.next = node; head = head.next; } return dummyHead.next; } }