Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java transform nodes into pairwise merging

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how Java converts nodes into a pairwise merger". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how Java converts nodes into a pairwise merger".

/ * * convert to pairwise merger * / 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 = N2; ReverseKGroup mt = new ReverseKGroup () ListNode head = mt.reverseKGroup (L1null 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) {/ / flip the first node reverseList (first, last) of the next combination nextGroup = last.next; / / when k combinations are satisfied; preGroup.next = last; / / last is already the first node preGroup = first after flipping / / preGroup sets itself to be the last, after which first continues to the next combination, using the deep meaning of preGroup, which is not to disturb first to continue to the next combination 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 At this point, I believe you have a deeper understanding of "how Java converts nodes into a pairwise merger". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report