Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for singly-linked list.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int x) { val = x; }
- * }
- */
- class Solution {
- public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
- ListNode n = null;
- ListNode N = null;
- if (l1 == null){
- return l2;
- }
- else if (l2 == null){
- return l1;
- }
- if (l1 != null && l2 != null){
- if (l1.val < l2.val){
- n = l1;
- l1 = l1.next;
- N = n;
- }
- else if (l1.val > l2.val){
- n = l2;
- l2 = l2.next;
- N = n;
- }
- else if (l1.val == l2.val){
- n = l1;
- l1 = l1.next;
- N = n;
- N.next = l2;
- N = l2;
- l2 = l2.next;
- }
- }
- while (l1 != null && l2 != null){
- if (l1.val < l2.val){
- N.next = l1;
- N = l1;
- l1 = l1.next;
- }
- else if (l1.val > l2.val){
- N.next = l2;
- N = l2;
- l2 = l2.next;
- }
- else if (l1.val == l2.val){
- N.next = l1;
- N = l1;
- l1 = l1.next;
- N.next = l2;
- N = l2;
- l2 = l2.next;
- }
- }
- while (l1 != null){
- N.next = l1;
- N = l1;
- l1 = l1.next;
- }
- while (l2 != null){
- N.next = l2;
- N = l2;
- l2 = l2.next;
- }
- return n;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement