Skip to content

Commit

Permalink
Merge pull request #95 from kajurampatell/patch-4
Browse files Browse the repository at this point in the history
 Merge Two Sorted Lists
  • Loading branch information
avastino7 authored Nov 1, 2022
2 parents f773537 + de3c3ed commit 43fc520
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions ListNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {

ListNode dummyNode = new ListNode(0);
ListNode tail = dummyNode;
while(true)
{
if(list1 == null)
{
tail.next = list2;
break;
}
if(list2 == null)
{
tail.next = list1;
break;
}

if(list1.val <= list2.val)
{
tail.next = list1;
list1 = list1.next;
}
else
{
tail.next = list2;
list2 = list2.next;
}
tail = tail.next;
}
return dummyNode.next;

}
}

0 comments on commit 43fc520

Please sign in to comment.