## MergeTwoSortedLists.java ( File view )

• By issaczr 2014-10-24
• View(s)：52
• Point(s)： 1
```			// Author:   Li Long, 1988lilong@163.com
// Date:     Apr 17, 2014
// Source:   http://oj.leetcode.com/problems/merge-two-sorted-lists/
// Analysis: http://blog.csdn.net/lilong_dream/article/details/20075857

// 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.

/**
* public class ListNode {

*     int val;
*     ListNode next;
*     ListNode(int x) {

*         val = x;
*         next = null;
*
}
*
}
*/

public class MergeTwoSortedLists {

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {

if (l1 == null) {

return l2;

}
if (l2 == null) {

return l1;

}

ListNode node = null;

while (l1 != null && l2 != null) {

if (l1.val <= l2.val) {

if (node == null) {

node = l1;

} else {

node.next = l1;
node = node.next;

}

l1 = l1.next;

} else {

if (node == null) {

node = l2;

} else {

node.next = l2;
node = node.next;

}

l2 = l2.next;

}

}

if (l1 != null) {

node.next = l1;

} else if (l2 != null) {

node.next = l2;

}

}

public void printList(ListNode node) {

while (node != null) {

System.out.print(node.val + "->");
node = node.next;

}
System.out.println(" ");

}

public static void main(String[] args) {

ListNode a1 = new ListNode(1);
ListNode a2 = new ListNode(3);
ListNode a3 = new ListNode(7);
a1.next = a2;
a2.next = a3;

ListNode b1 = new ListNode(2);
ListNode b2 = new ListNode(4);
b1.next = b2;

MergeTwoSortedLists slt = new MergeTwoSortedLists();
ListNode res = slt.mergeTwoLists(a1, b1);
slt.printList(res);

}

}
```
...
Expand＞ ＜Close

Point(s): 1

0 lines left, continue to read

## File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.97 kB
01.97 kB
BinaryTreePreorderTraversal.java1.91 kB2014-05-09 03:48
ClimbingStairs.java811.00 B2014-05-09 03:48
EvaluateReversePolishNotation.java1.51 kB2014-05-09 03:48
GenerateParentheses.java1.15 kB2014-05-09 03:48
ImplementStrStr.java1.00 kB2014-05-09 03:48
InsertionSortList.java1.64 kB2014-05-09 03:48
IntegerToRoman.java923.00 B2014-05-09 03:48
LengthOfLastWord.java1.11 kB2014-05-09 03:48
LongestCommonPrefix.java916.00 B2014-05-09 03:48
LongestSubstring.java1.79 kB2014-05-09 03:48
MaximumSubarray.java1.10 kB2014-05-09 03:48
MedianOfTwoSortedArrays.java1.68 kB2014-05-09 03:48
MergeSortedArray.java1.12 kB2014-05-09 03:48
MergeTwoSortedLists.java1.84 kB2014-05-09 03:48
MinimumPathSum.java1.19 kB2014-05-09 03:48
PathSum.java1.67 kB2014-05-09 03:48
PlusOne.java1.06 kB2014-05-09 03:48
Powxn.java712.00 B2014-05-09 03:48
RemoveDuplicatesFromSortedList.java1.48 kB2014-05-09 03:48
RemoveDuplicatesfromSortedArray.java1.30 kB2014-05-09 03:48
RemoveElement.java885.00 B2014-05-09 03:48
RemoveNthNodeFromEnd.java1.70 kB2014-05-09 03:48
ReverseInteger.java936.00 B2014-05-09 03:48
RotateList.java1.53 kB2014-05-09 03:48
SameTree.java1.15 kB2014-05-09 03:48
SearchForARange.java1.37 kB2014-05-09 03:48
SearchInRotatedSortedArray.java1.27 kB2014-05-09 03:48
SearchInsertPosition.java1.09 kB2014-05-09 03:48
SetMatrixZeroes.java1.95 kB2014-05-09 03:48
SingleNumber.java935.00 B2014-05-09 03:48
SortList.java1.75 kB2014-05-09 03:48
Sqrt.java907.00 B2014-05-09 03:48
StringToInteger_atoi.java1.46 kB2014-05-09 03:48
Sum3.java1.76 kB2014-05-09 03:48
TwoSum.java2.33 kB2014-05-09 03:48
UniqueBinarySearchTrees.java1.01 kB2014-05-09 03:48
UniquePaths.java976.00 B2014-05-09 03:48
UniquePathsII.java1.84 kB2014-05-09 03:48
ValidParentheses.java1.23 kB2014-05-09 03:48
ValidateBinarySearchTree.java1.44 kB2014-05-09 03:48
...
• Sent successfully!
• LeetCode-Java-master.zip
• 1 point

### MergeTwoSortedLists.java (35.66 kB)

Need 1 point

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

Don't have an account？ Register now
Need any help?
Mail to: support@codeforge.com

### 切换到中文版？

CodeForge Chinese Version
CodeForge English Version

### ^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!

### Warm tip!

Favorite by Ctrl+D