File tree 6 files changed +266
-7
lines changed
6 files changed +266
-7
lines changed Original file line number Diff line number Diff line change
1
+ '''
2
+ Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
3
+
4
+ Note: Do not modify the linked list.
5
+
6
+ Follow up:
7
+ Can you solve it without using extra space?
8
+ '''
9
+ # Definition for singly-linked list.
10
+ # class ListNode(object):
11
+ # def __init__(self, x):
12
+ # self.val = x
13
+ # self.next = None
14
+
15
+ class Solution (object ):
16
+ def detectCycle (self , head ):
17
+ """
18
+ :type head: ListNode
19
+ :rtype: ListNode
20
+ """
21
+ if not head :
22
+ return None
23
+
24
+ slow , fast = head , head .next
25
+
26
+ while fast and fast .next :
27
+ slow = slow .next
28
+ fast = fast .next .next
29
+ if slow == fast :
30
+ break
31
+
32
+ if slow == fast :
33
+ slow = head
34
+ while slow != fast :
35
+ slow = slow .next
36
+ fast = fast .next
37
+ return slow
38
+ return None
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
+ class Solution (object ):
8
+ def reorderList (self , head ):
9
+ """
10
+ :type head: ListNode
11
+ :rtype: void Do not return anything, modify head in-place instead.
12
+ """
13
+ if not head :
14
+ return None
15
+
16
+ slow , fast = head , head .next
17
+
18
+ while fast and fast .next :
19
+ slow = slow .next
20
+ fast = fast .next .next
21
+
22
+ head1 , head2 = head , slow .next
23
+ slow .next = None
24
+ prev = None
25
+ curr = head2
26
+ while curr :
27
+ nex = curr .next
28
+ curr .next = prev
29
+ prev = curr
30
+ curr = nex
31
+ head2 = prev
32
+
33
+ while head2 :
34
+ n1 = head1 .next
35
+ n2 = head2 .next
36
+ head1 .next = head2
37
+ head1 .next .next = n1
38
+ head2 = n2
39
+ head1 = head1 .next .next
40
+
41
+ head = head1
Original file line number Diff line number Diff line change
1
+ '''
2
+ Given a binary tree, return the preorder traversal of its nodes' values.
3
+
4
+ Example:
5
+
6
+ Input: [1,null,2,3]
7
+ 1
8
+ \
9
+ 2
10
+ /
11
+ 3
12
+
13
+ Output: [1,2,3]
14
+ Follow up: Recursive solution is trivial, could you do it iteratively?
15
+ '''
16
+
17
+ # Definition for a binary tree node.
18
+ # class TreeNode(object):
19
+ # def __init__(self, x):
20
+ # self.val = x
21
+ # self.left = None
22
+ # self.right = None
23
+
24
+ class Solution (object ):
25
+ def preorderTraversal (self , root ):
26
+ """
27
+ :type root: TreeNode
28
+ :rtype: List[int]
29
+ """
30
+ if not root :
31
+ return []
32
+
33
+ stack , result = [root ], []
34
+ while stack :
35
+ element = stack .pop ()
36
+ result .append (element .val )
37
+
38
+ if element .right :
39
+ stack .append (element .right )
40
+ if element .left :
41
+ stack .append (element .left )
42
+
43
+ return result
44
+
45
+
46
+ # Definition for a binary tree node.
47
+ # class TreeNode(object):
48
+ # def __init__(self, x):
49
+ # self.val = x
50
+ # self.left = None
51
+ # self.right = None
52
+
53
+ class Solution (object ):
54
+ def preorderTraversal (self , root ):
55
+ """
56
+ :type root: TreeNode
57
+ :rtype: List[int]
58
+ """
59
+ result = []
60
+
61
+ def recursive (root , result ):
62
+ if not root :
63
+ return
64
+ result .append (root .val )
65
+ recursive (root .left , result )
66
+ recursive (root .right , result )
67
+
68
+ recursive (root , result )
69
+ return result
Original file line number Diff line number Diff line change
1
+ '''
2
+ Given a binary tree, return the postorder traversal of its nodes' values.
3
+
4
+ Example:
5
+
6
+ Input: [1,null,2,3]
7
+ 1
8
+ \
9
+ 2
10
+ /
11
+ 3
12
+
13
+ Output: [3,2,1]
14
+ Follow up: Recursive solution is trivial, could you do it iteratively?
15
+ '''
16
+
17
+ # Definition for a binary tree node.
18
+ # class TreeNode(object):
19
+ # def __init__(self, x):
20
+ # self.val = x
21
+ # self.left = None
22
+ # self.right = None
23
+
24
+ class Solution (object ):
25
+ def postorderTraversal (self , root ):
26
+ """
27
+ :type root: TreeNode
28
+ :rtype: List[int]
29
+ """
30
+
31
+ result = []
32
+
33
+ def recursive (root , result ):
34
+ if not root :
35
+ return
36
+ recursive (root .left , result )
37
+ recursive (root .right , result )
38
+ result .append (root .val )
39
+ recursive (root , result )
40
+ return result
41
+
42
+
43
+ # Definition for a binary tree node.
44
+ # class TreeNode(object):
45
+ # def __init__(self, x):
46
+ # self.val = x
47
+ # self.left = None
48
+ # self.right = None
49
+
50
+ class Solution (object ):
51
+ def postorderTraversal (self , root ):
52
+ """
53
+ :type root: TreeNode
54
+ :rtype: List[int]
55
+ """
56
+
57
+ if not root :
58
+ return []
59
+
60
+ stack , result = [], []
61
+
62
+ while True :
63
+ while root :
64
+ if root .right :
65
+ stack .append (root .right )
66
+ stack .append (root )
67
+ root = root .left
68
+
69
+ root = stack .pop ()
70
+
71
+ if root .right and stack and stack [- 1 ] == root .right :
72
+ stack .pop ()
73
+ stack .append (root )
74
+ root = root .right
75
+ else :
76
+ result .append (root .val )
77
+ root = None
78
+
79
+ if len (stack )<= 0 :
80
+ break
81
+
82
+ return result
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
+ class Solution (object ):
8
+ def insertionSortList (self , head ):
9
+ """
10
+ :type head: ListNode
11
+ :rtype: ListNode
12
+ """
13
+
14
+ if not head :
15
+ return None
16
+
17
+ sortedList = head
18
+ head = head .next
19
+ sortedList .next = None
20
+
21
+ while head :
22
+ curr = head
23
+ head = head .next
24
+ if curr .val <= sortedList .val :
25
+ curr .next = sortedList
26
+ sortedList = curr
27
+ else :
28
+ temp = sortedList
29
+ while temp .next and temp .next .val < curr .val :
30
+ temp = temp .next
31
+ curr .next = temp .next
32
+ temp .next = curr
33
+ return sortedList
Original file line number Diff line number Diff line change @@ -16,10 +16,6 @@ def firstUniqChar(self, s):
16
16
:type s: str
17
17
:rtype: int
18
18
"""
19
- if not s :
20
- return - 1
21
-
22
- for index , char in enumerate (s ):
23
- if s .count (char ) == 1 :
24
- return index
25
- return - 1
19
+ letters = 'abcdefghijklmnopqrstuvwxyz'
20
+ index = [s .index (l ) for l in letters if s .count (l ) == 1 ]
21
+ return min (index ) if len (index ) > 0 else - 1
You can’t perform that action at this time.
0 commit comments