//将单链表的每K个节点之间逆序
public Node reverseKNodes( Node head, int k) {
if( k<2) return head;
Node cur = head;
Node start = null;
Node pre = null;
Node next = null;
int count = 1;
while( cur != null) {
next = cur.next;
if( count==k) {
start = pre==null ? head:pre.next;
head = pre==null ? cur:head;
resign( pre, start, cur, next);
pre = start;
count=0;
}
count++;
cur = next;
}
return head;
}
public void resign( Node left, Node start, Node end, Node right) {
Node pre = start;
Node cur = start.next;
Node next = null;
while( cur!=right) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
if( left!=null)
left.next = end;
start.next = right;
}
转载于:https://my.oschina.net/u/2009816/blog/732564