public class Solution { ListNode root, tp = new ListNode(0); public ListNode ReverseList(ListNode head) { root = tp; dfs(head); tp.next = null; return root.next; } public void dfs(ListNode head) { if(head != null) { dfs(head.next); tp.next = head; tp = tp.next; } } }
public ListNode ReverseList(ListNode head) { if (head == null || head.next == null) return head; ListNode pre = null, next = null; while (head.next != null) { next = head.next; head.next = pre; pre = head; head = next; } head.next = pre; return head; }