单链表:
public class ListNode { int val; ListNode next; ListNode (int val) { this.val = val; } }
1、反转单链表
public void reverseLinkedList(ListNode head) { ListNode pre = null; while (head != null) { ListNode next = head.next; head.next = pre; pre = head; head = next; } return pre; }
2、寻找单链表的中点
public ListNode findMid(ListNode head) { ListNode slow = head; ListNode fast = head; while (fast != null && fast.next != null) { fast = fast.next.next; slow = slow.next; } return slow; }
时间: 2024-10-12 08:02:24