25. K个一组翻转链表
遇到困难直接看题解 --_--,每k个一组,进行翻转,然后再装回原链表中。
{ ListNode reverseKGroup(ListNode head, k) { ListNode hair ListNode(); hair.next head; ListNode pre hair; (head ) { ListNode tail pre; ( i ; i k; i) { tail tail.next; (tail ) hair.next; } ListNode nex tail.next; ListNode[] reverse myReverse(head,tail); head reverse[]; tail reverse[]; pre.next head; tail.next nex; pre tail; head tail.next; } hair.next; } ListNode[] myReverse(ListNode head, ListNode tail) { ListNode prev tail.next; ListNode p head; (prev tail) { ListNode nex p.next; p.next prev; prev p; p nex; } ListNode[] {tail, head}; } }