Get the App
SLTechnology News&Howtos  ›  Servers  › 

How does Java transform nodes into pairwise merging

Shulou Source: shulou.com Published: 2022-05-31 17:55:50 09月20日 Update

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!

Tags: Nodes combinations content learning practical deeper interest practicality practicality simple operation methods more friends deep meaning websites channels inquiries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Microsoft Redmi macOS NVidia