难度:简单/Easy
序号与题目:21——合并两个有序链表
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
思考:如果L1为空,返回L2;如果L2为空,返回L1;如果L1和L2都为空,返回空;定义新链L,同时从L1和L2链表的头节点开始遍历,比较当前节点大小,将小的节点添加到L链表中,然后遍历。当L1为空时,L连接剩下的L2;当L2为空时,L连接剩下的L1
实现:
C
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2)
{
struct ListNode*l,*head;
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1;
if(l1->val < l2->val)
{
l = l1;
l1 = l1->next;
}
else
{
l = l2;
l2 = l2->next;
}
head = l;
while(l1 && l2)
{
if(l1->val < l2->val)
{
l->next = l1;
l = l1;
l1 = l1->next;
}
else
{
l->next = l2;
l = l2;
l2 = l2->next;
}
}
if(l1 == NULL)
l->next = l2;
else
l->next = l1;
return head;
}
C++
/**
* 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)
{
struct ListNode*l,*head;
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1;
if(l1->val < l2->val)
{
l = l1;
l1 = l1->next;
}
else
{
l = l2;
l2 = l2->next;
}
head = l;
while(l1 && l2)
{
if(l1->val < l2->val)
{
l->next = l1;
l = l1;
l1 = l1->next;
}
else
{
l->next = l2;
l = l2;
l2 = l2->next;
}
}
if(l1 == NULL)
l->next = l2;
else
l->next = l1;
return head;
}
};
Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution
{
public ListNode mergeTwoLists(ListNode l1, ListNode l2)
{
if (l1 == null) return l2;
if (l2 == null) return l1;
ListNode head = new ListNode(1), temp = head;
while (l1 != null && l2 != null)
{
if (l1.val <= l2.val)
{
temp.next = l1;
l1 = l1.next;
} else
{
temp.next = l2;
l2 = l2.next;
}
temp = temp.next;
temp.next = null;
}
temp.next = (l1 == null ? l2 : l1);
return head.next;
}
}
Python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if l1==None: return l2
if l2==None: return l1
if l1.val<=l2.val:
head=ListNode(l1.val)
l1=l1.next
else:
head=ListNode(l2.val)
l2=l2.next
currentnode=head
while (l1!=None)&(l2!=None):
if l1.val<=l2.val:
currentnode.next=ListNode(l1.val)
l1=l1.next
currentnode=currentnode.next
else:
currentnode.next=ListNode(l2.val)
l2=l2.next
currentnode=currentnode.next
if l1!=None: currentnode.next=l1
if l2!=None: currentnode.next=l2
return head
序号与题目:26——删除排序数组的重复项
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
示例 1:
给定数组 nums = [1,1,2],
函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。
你不需要考虑数组中超出新长度后面的元素。
示例 2:
给定 nums = [0,0,1,1,1,2,2,3,3,4],
函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。
你不需要考虑数组中超出新长度后面的元素。
思考:数组完成排序后,我们可以放置两个指针i和j,其中i是慢指针,而 j是快指针。只要nums[i] = nums[j],我们就增加j以跳过重复项。当我们遇到nums[j] !=nums[i] 时,跳过重复项的运行已经结束,因此我们必须把nums[j]的值复制到nums[i+1]。然后递增i,接着我们将再次重复相同的过程,直到j到达数组的末尾为止。
实现:
C
int removeDuplicates(int* nums, int numsSize)
{
if(numsSize==0)
return 0;
int i=0;
for(int j=1;j<numsSize;j++)
{
if(nums[j]!=nums[i])
{
i++;
nums[i]=nums[j];
}
}
return i+1;
}
C++
class Solution
{
public:
int removeDuplicates(vector<int>& nums)
{
if(nums.size()==0)
return 0;
int i=0;
for(int j=1;j<nums.size();j++)
{
if(nums[j]!=nums[i])
{
i++;
nums[i]=nums[j];
}
}
return i+1;
}
};
Java
class Solution
{
public int removeDuplicates(int[] nums)
{
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++)
{
if (nums[j] != nums[i])
{
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
}
Python
class Solution:
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
lens = 1
if len(nums)==0:
return 0
for i in range(1,len(nums)):
if nums[i] != nums[i-1]:
nums[lens] = nums[i]
lens +=1
return lens