Last active
May 17, 2026 11:42
-
-
Save thinkphp/ac3234bc36fe5770d93b06b44d65b14d to your computer and use it in GitHub Desktop.
merge-sorted-linkedlists.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 1 -> 2 -> 4 | |
| 1 -> 3 -> 4 | |
| */ | |
| class ListNode { | |
| int val; | |
| ListNone next; | |
| ListNode() { | |
| this.val = null; | |
| this.next = null | |
| } | |
| } | |
| ListNode aux = new ListNode();//0x12132//se creeaza o lista simplu inlantuita //initial NULL | |
| ListNode curr = aux; //curr = 0x12132 //retine adresa lui aux | |
| /* | |
| list1: 1 -> 2 -> 4 | |
| list2: 1 -> 3 -> 4 | |
| */ | |
| while(list1 != null and list2 != null) { | |
| if(list1.val > lists2.val) { | |
| curr.next = list2; | |
| list2 = list2.next | |
| } else { | |
| curr.next = list1; | |
| list1 = list1.next; | |
| } | |
| curr = curr.next; | |
| } | |
| curr.next = (list1 != null) ? list1 : list2; | |
| return aux.next; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment