本篇内容主要讲解“Java怎么将节点转化为两两合并”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java怎么将节点转化为两两合并”吧!
/** * * 转化为两两合并 */ public class ReverseKGroup { public static void main(String[] args) { ListNode l1 = new ListNode(1); ListNode n1 = new ListNode(3); ListNode n2 = new ListNode(4); ListNode n3 = new ListNode(5); l1.next = n1; n1.next = n2; n2.next = n3; ReverseKGroup mt = new ReverseKGroup(); ListNode head = mt.reverseKGroup(l1,2); Utils.print(head); } public ListNode reverseKGroup(ListNode head, int k) { if (k == 1 || head == null || head.next == null) return head; ListNode preHead = new ListNode(-1); preHead.next = head; ListNode first = head, last = head; ListNode preGroup = preHead, nextGroup = preHead; int count = 1; while (last != null) { if (count == k) { //当满足k个组合时进行翻转 nextGroup = last.next; //下一个组合的首节点 reverseList(first, last); preGroup.next = last; //翻转之后last已经是首节点 preGroup = first; //preGroup设置自己为最末,此后first继续下一个组合,采用preGroup的深意,是不打扰first继续下一个组合 first.next = nextGroup; first = nextGroup; last = nextGroup; count = 1; continue; } last = last.next; count++; } return preHead.next; } private void reverseList(ListNode head, ListNode tail) { ListNode pre = new ListNode(-1), node = head; pre.next = head; while (pre != tail) { ListNode temp = node.next; node.next = pre; pre = node; node = temp; } } }
到此,相信大家对“Java怎么将节点转化为两两合并”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。