Get the App
SLTechnology News&Howtos  ›  Servers  › 

How does Java return to the first node?

Shulou Source: shulou.com Published: 2022-05-31 17:56:43 10月03日 Update

This article mainly explains "how to return Java to the first node". 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 to get Java back to the first node"!

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2-> 4-> 3) + (5-> 6-> 4) Output: 7-> 0-> 8 Definition for singly-linked list. * public class ListNode {* int val; * ListNode next; * ListNode (int x) {val = x;} *} * / public class AddTwoNumbers {public static void main (String [] args) {ListNode L1 = new ListNode (2); ListNode l1n1 = new ListNode (4); ListNode l1n2 = new ListNode (3); ListNode L2 = new ListNode (5); ListNode l2n1 = new ListNode (6) ListNode l2n2 = new ListNode (4); l1.next = 11n1; l1n1.next = L1N2; l2.next = L2N1; l2n1.next = L2N2; AddTwoNumbers atn = new AddTwoNumbers (); ListNode pre = atn.addTwoNumbers (L1SCH L2); Utils.print (pre);} public ListNode addTwoNumbers (ListNode L1, ListNode L2) {int carry = 0; ListNode newHead = new ListNode (0) / / as ListNode p1 = L1, p2 = L2, p3 newhead; / / p3 as operation variable while (p1! = null | | p2! = null) {if (p1! = null) {carry + = p1.vale; p1 = p1.nextt;} if (p2! = null) {carry + = p2.val P2 = p2.next;} p3.next=new ListNode (carry); p3 = p3.next; carry / = 10;} if (carry==1) p3.next=new ListNode (1); return newHead.next / / return to the first node}} so far, I believe you have a deeper understanding of "how to return the Java to the first node". 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: Node content learning practical deeper interest variable practical practical easy to operate method more friends website channel query Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MariaDB Redmi Xiaomi Shulou Information