Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions merge-two-sorted-lists/hjeomdev.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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 list = new ListNode(-1);
ListNode result = list;
while (list1 != null && list2 != null) {
if (list1.val < list2.val) {
result.next = list1;
list1 = list1.next;
} else {
result.next = list2;
list2 = list2.next;
}
result = result.next;
}
result.next = list1 != null ? list1 : list2;
return list.next;
}
}