File tree Expand file tree Collapse file tree 3 files changed +91
-0
lines changed
082.Remove Duplicates from Sorted List II
083.Remove Duplicates from Sorted List Expand file tree Collapse file tree 3 files changed +91
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Definition for singly-linked list.
2
+ # class ListNode:
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.next = None
6
+
7
+ class Solution :
8
+ def mergeKLists (self , lists ):
9
+ """
10
+ :type lists: List[ListNode]
11
+ :rtype: ListNode
12
+ """
13
+ list0 = []
14
+ for i in lists :
15
+ while i :
16
+ list0 .append (i .val )
17
+ i = i .next
18
+ if list0 == []:
19
+ return []
20
+ list0 .sort (reverse = True )
21
+ ln = ListNode (0 )
22
+ tmp = ln
23
+ while list0 :
24
+ tmp1 = list0 .pop ()
25
+ ln .next = ListNode (tmp1 )
26
+ ln = ln .next
27
+ return tmp .next
Original file line number Diff line number Diff line change
1
+ # Definition for singly-linked list.
2
+ # class ListNode:
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.next = None
6
+
7
+ class Solution :
8
+ def deleteDuplicates (self , head ):
9
+ """
10
+ :type head: ListNode
11
+ :rtype: ListNode
12
+ """
13
+ if head == None :
14
+ return None
15
+ list0 = list ()
16
+ i = head
17
+ while i :
18
+ list0 .append (i .val )
19
+ i = i .next
20
+ list1 = list0 .copy ()
21
+ for i in list0 :
22
+ c = list0 .count (i )
23
+ if c != 1 :
24
+ while 1 :
25
+ try :
26
+ list1 .remove (i )
27
+ except :
28
+ break
29
+
30
+ new_listnode = ListNode (0 )
31
+ j = new_listnode
32
+ for v in list1 :
33
+ new_listnode .next = ListNode (v )
34
+ new_listnode = new_listnode .next
35
+ return j .next
Original file line number Diff line number Diff line change
1
+ # Definition for singly-linked list.
2
+ # class ListNode:
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.next = None
6
+
7
+ class Solution :
8
+ def deleteDuplicates (self , head ):
9
+ """
10
+ :type head: ListNode
11
+ :rtype: ListNode
12
+ """
13
+ if head == None :
14
+ return None
15
+ dict0 = dict ()
16
+ i = head
17
+ c = 0
18
+ while i :
19
+ if i .val not in dict0 .values ():
20
+ dict0 [c ]= i .val
21
+ c += 1
22
+ i = i .next
23
+ new_listnode = ListNode (0 )
24
+ j = new_listnode
25
+ for v in dict0 .values ():
26
+ new_listnode .next = ListNode (v )
27
+ new_listnode = new_listnode .next
28
+ return j .next
29
+
You can’t perform that action at this time.
0 commit comments