Skip to content

Commit e7c7d6d

Browse files
committed
21. Merge Two Sorted Lists
1 parent e243224 commit e7c7d6d

File tree

7 files changed

+140
-24
lines changed

7 files changed

+140
-24
lines changed

src/leetcode/_0_template/Main.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package leetcode._0_template;
22

3-
import leetcode._19_.ListNode;
4-
53
/**
64
* Created by zhangbo54 on 2019-03-04.
75
*/

src/leetcode/_19_/Main.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package leetcode._19_;
22

3+
import leetcode.common.ListNode;
4+
35
/**
46
* Created by zhangbo54 on 2019-03-04.
57
*/

src/leetcode/_19_/Solution.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package leetcode._19_;
22

3-
import java.util.ArrayList;
4-
import java.util.Arrays;
5-
import java.util.Collections;
6-
import java.util.HashSet;
7-
import java.util.List;
8-
import java.util.Set;
3+
import leetcode.common.ListNode;
94

105
/**
116
* Definition for singly-linked list.

src/leetcode/_21_/Main.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode._21_;
2+
3+
import leetcode.common.ListNode;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
ListNode node = new ListNode(1).setNext(new ListNode(2).setNext(new ListNode(4)));
12+
ListNode node2 = new ListNode(1).setNext(new ListNode(3).setNext(new ListNode(4)));
13+
System.out.println(solution.mergeTwoLists(node, node2));
14+
}
15+
}
16+

src/leetcode/_21_/Solution.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package leetcode._21_;
2+
3+
import leetcode.common.ListNode;
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* public class ListNode {
8+
* int val;
9+
* ListNode next;
10+
* ListNode(int x) { val = x; }
11+
* }
12+
*/
13+
class Solution {
14+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
15+
if (l1 == null) {
16+
return l2;
17+
}
18+
if (l2 == null) {
19+
return l1;
20+
}
21+
int headVal = 0;
22+
if (l1.val > l2.val) {
23+
headVal = l2.val;
24+
l2 = l2.next;
25+
} else {
26+
headVal = l1.val;
27+
l1 = l1.next;
28+
}
29+
ListNode head = new ListNode(headVal);
30+
ListNode index = head;
31+
while (l1 != null && l2 != null) {
32+
if (l1.val > l2.val) {
33+
index.next = new ListNode(l2.val);
34+
l2 = l2.next;
35+
} else {
36+
index.next = new ListNode(l1.val);
37+
l1 = l1.next;
38+
}
39+
index = index.next;
40+
}
41+
if (l1 != null) {
42+
index.next = l1;
43+
}
44+
if (l2 != null) {
45+
index.next = l2;
46+
}
47+
return head;
48+
}
49+
}

src/leetcode/_21_/solution.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
### [21\. Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
7+
8+
**Example:**
9+
10+
```
11+
Input: 1->2->4, 1->3->4
12+
Output: 1->1->2->3->4->4
13+
```
14+
15+
16+
#### Solution
17+
18+
Language: **Java**
19+
20+
```java
21+
22+
/**
23+
* Definition for singly-linked list.
24+
* public class ListNode {
25+
* int val;
26+
* ListNode next;
27+
* ListNode(int x) { val = x; }
28+
* }
29+
*/
30+
class Solution {
31+
   public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
32+
       if (l1 == null) {
33+
           return l2;
34+
      }
35+
       if (l2 == null) {
36+
           return l1;
37+
      }
38+
       int headVal = 0;
39+
       if (l1.val > l2.val) {
40+
           headVal = l2.val;
41+
           l2 = l2.next;
42+
      } else {
43+
           headVal = l1.val;
44+
           l1 = l1.next;
45+
      }
46+
       ListNode head = new ListNode(headVal);
47+
       ListNode index = head;
48+
       while (l1 != null && l2 != null) {
49+
           if (l1.val > l2.val) {
50+
               index.next = new ListNode(l2.val);
51+
               l2 = l2.next;
52+
          } else {
53+
               index.next = new ListNode(l1.val);
54+
               l1 = l1.next;
55+
          }
56+
           index = index.next;
57+
      }
58+
       if (l1 != null) {
59+
           index.next = l1;
60+
      }
61+
       if (l2 != null) {
62+
           index.next = l2;
63+
      }
64+
       return head;
65+
  }
66+
}
67+
```
68+
![](https://ws4.sinaimg.cn/large/006tKfTcgy1g0ui2op9ucj31180scgpp.jpg)

src/leetcode/_19_/ListNode.java renamed to src/leetcode/common/ListNode.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
1-
package leetcode._19_;
1+
package leetcode.common;
22

33
/**
44
* Created by zhangbo54 on 2019-03-06.
55
*/
66
public class ListNode {
7-
int val;
8-
ListNode next;
7+
public int val;
8+
public ListNode next;
99

10-
ListNode(int x) {
10+
public ListNode(int x) {
1111
val = x;
1212
}
1313

14-
public int getVal() {
15-
return val;
16-
}
17-
18-
public void setVal(int val) {
19-
this.val = val;
20-
}
21-
22-
public ListNode getNext() {
23-
return next;
24-
}
25-
2614
public ListNode setNext(ListNode next) {
2715
this.next = next;
2816
return this;

0 commit comments

Comments
 (0)