File tree 2 files changed +33
-0
lines changed
021.Merge Two Sorted Lists
026.Remove Duplicates from Sorted Array
2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Definition for singly-linked list.
2
+ # class ListNode(object):
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.next = None
6
+
7
+ #经典的merge,每次都比较两个链表的第一个数字
8
+ #递归的方法
9
+ class Solution (object ):
10
+ def mergeTwoLists (self , l1 , l2 ):
11
+ if not l1 or not l2 :
12
+ return l1 or l2
13
+ if l1 .val < l2 .val : #输出较小的一个
14
+ l1 .next = self .mergeTwoLists (l1 .next , l2 )
15
+ return l1
16
+ else :
17
+ l2 .next = self .mergeTwoLists (l1 , l2 .next )
18
+ return l2
19
+
Original file line number Diff line number Diff line change
1
+ #48ms
2
+ class Solution (object ):
3
+ def removeDuplicates (self , nums ):
4
+ if not nums :
5
+ return 0
6
+
7
+ newtail = 0
8
+
9
+ for i in range (1 , len (nums )):
10
+ if nums [i ] != nums [newtail ]:
11
+ newtail += 1
12
+ nums [newtail ] = nums [i ] #只要求字符串前段正确即可
13
+
14
+ return newtail + 1 #要求返回新字符串长度
You can’t perform that action at this time.
0 commit comments