Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit93a4f4d

Browse files
author
applewjg
committed
Merge Two Sorted Lists
Change-Id: I11df25864eb72113f93f7a69c2121ca01179b96b
1 parente304095 commit93a4f4d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

‎MergeTwoSortedLists.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Jan 2, 2015
4+
Problem: Merge Two Sorted Lists
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/merge-two-sorted-lists/
7+
Notes:
8+
Merge two sorted linked lists and return it as a new list.
9+
The new list should be made by splicing together the nodes of the first two lists.
10+
11+
Solution: ...
12+
*/
13+
14+
/**
15+
* Definition for singly-linked list.
16+
* public class ListNode {
17+
* int val;
18+
* ListNode next;
19+
* ListNode(int x) {
20+
* val = x;
21+
* next = null;
22+
* }
23+
* }
24+
*/
25+
publicclassSolution {
26+
publicListNodemergeTwoLists(ListNodel1,ListNodel2) {
27+
ListNodehead =newListNode(0);
28+
ListNodecur =head;
29+
while (l1 !=null &&l2 !=null) {
30+
if (l1.val <l2.val) {
31+
cur.next =l1;
32+
l1 =l1.next;
33+
}else {
34+
cur.next =l2;
35+
l2 =l2.next;
36+
}
37+
cur =cur.next;
38+
}
39+
if (l1 !=null)cur.next =l1;
40+
if (l2 !=null)cur.next =l2;
41+
returnhead.next;
42+
}
43+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp