Leetcode 21. Merge Two Sorted Lists (Easy) (cpp)
2016-07-12 12:44
465 查看
Leetcode 21. Merge Two Sorted Lists (Easy) (cpp)
Tag: Linked List
Difficulty: Easy
Tag: Linked List
Difficulty: Easy
/* 21. Merge Two Sorted Lists (Easy) 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. */ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* prev = new ListNode(-1); for (ListNode* p = prev;l1 != NULL || l2 != NULL; p = p -> next) { int num1 = l1 == NULL? INT_MAX : l1 -> val; int num2 = l2 == NULL? INT_MAX : l2 -> val; if (num1 >= num2) { p -> next = l2; l2 = l2 -> next; } else { p -> next = l1; l1 = l1 -> next; } } return prev -> next; } };
相关文章推荐
- Leetcode 290. Word Pattern (Easy) (cpp)
- Leetcode 299. Bulls and Cows (Easy) (cpp)
- C++中的引用
- 【BZOJ1977】【BJOI2011】严格次小生成树
- Cpp环境【BZOJ1626】【Usaco2007 Dec】【Vijos 1693】 修建道路
- Leetcode 242. Valid Anagram (Easy) (cpp)
- Leetcode 350. Intersection of Two Arrays II (Easy) (cpp)
- Leetcode 349. Intersection of Two Arrays (Easy) (cpp)
- Leetcode 36. Valid Sudoku (Easy) (cpp)
- Leetcode 204. Count Primes (Easy) (cpp)
- Leetcode 66. Plus One (Easy) (cpp)
- Leetcode 121. Best Time to Buy and Sell Stock (Easy) (cpp)
- Leetcode 169. Majority Element (Easy) (cpp)
- C++中的auto_ptr智能指针的作用及使用方法详解
- Leetcode 283. Move Zeroes (Easy) (cpp)
- Leetcode 81. Search in Rotated Sorted Array II (Medium) (cpp)
- Leetcode 219. Contains Duplicate II (Easy) (cpp)
- Leetcode 217. Contains Duplicate (Easy) (cpp)
- Leetcode 189. Rotate Array (Easy) (cpp)
- Leetcode 88. Merge Sorted Array (Easy) (cpp)