143.重排链表 发表于 2020-10-20 | 分类于 算法 | 本文总阅读量 次 字数统计: 175 | 阅读时长 ≈ 1 题目描述 题解快慢指针 + 反转链表先通过快慢指针的方法找出链表的中间节点, 以此为边界把后半段链表反转, 然后交错插入前半段链表中 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748public class lc143 { public void reorderList(ListNode head) { if (head == null || head.next == null) { return; } ListNode slow = head; ListNode fast = head.next; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } ListNode head2 = reverse(slow.next); slow.next = null; ListNode pre = head; ListNode cur; ListNode cur2; while (head2 != null) { cur = pre.next; cur2 = head2.next; head2.next = pre.next; pre.next = head2; pre = cur; head2 = cur2; } System.out.println(head); } private ListNode reverse(ListNode head) { ListNode cur = head; ListNode pre = null; while (cur != null) { ListNode t = cur.next; cur.next = pre; pre = cur; cur = t; } return pre; }} -------------本文结束感谢您的阅读------------- 可以请我喝杯奶茶吗 打赏 微信支付 支付宝