// 单链表节点的结构
public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } }
递归反转整个链表
ListNode reverse(ListNode head) { if (head.next == null) return head; ListNode last = reverse(head.next); head.next.next = head; head.next = null; return last; }
boolean hasCycle(ListNode head) { ListNode fast, slow; fast = slow = head; while (fast != null && fast.next != null) { fast = fast.next.next; slow = slow.next; if (fast == slow) return true; } return false; }
No comments:
Post a Comment