File tree Expand file tree Collapse file tree 4 files changed +115
-9
lines changed
populating_next_right_pointers_in_each_node
remove_linked_list_elements Expand file tree Collapse file tree 4 files changed +115
-9
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Given a linked list, determine if it has a cycle in it.
3+
4+ Follow up:
5+ Can you solve it without using extra space?
6+ """
7+
18# Definition for singly-linked list.
2- # class ListNode:
9+ # class ListNode(object) :
310# def __init__(self, x):
411# self.val = x
512# self.next = None
613
7- class Solution :
8- # @param head, a ListNode
9- # @return a boolean
14+ class Solution (object ):
1015 def hasCycle (self , head ):
16+ """
17+ :type head: ListNode
18+ :rtype: bool
19+ """
1120 slow = head
1221 fast = head
1322 while fast is not None and fast .next is not None :
Original file line number Diff line number Diff line change 1+ """
2+ Given a singly linked list, determine if it is a palindrome.
3+
4+ Follow up:
5+ Could you do it in O(n) time and O(1) space?
6+ """
7+
8+ # Definition for singly-linked list.
9+ # class ListNode(object):
10+ # def __init__(self, x):
11+ # self.val = x
12+ # self.next = None
13+
14+ class Solution (object ):
15+ def isPalindrome (self , head ):
16+ """
17+ :type head: ListNode
18+ :rtype: bool
19+ """
Original file line number Diff line number Diff line change 1- # Definition for a binary tree node
2- # class TreeNode:
1+ """
2+ Given a binary tree
3+
4+ struct TreeLinkNode {
5+ TreeLinkNode *left;
6+ TreeLinkNode *right;
7+ TreeLinkNode *next;
8+ }
9+
10+ Populate each next pointer to point to its next right node. If there is no
11+ next right node, the next pointer should be set to NULL.
12+
13+ Initially, all next pointers are set to NULL.
14+
15+ Note:
16+
17+ You may only use constant extra space.
18+ You may assume that it is a perfect binary tree (ie, all leaves are at the
19+ same level, and every parent has two children).
20+
21+ For example,
22+
23+ Given the following perfect binary tree,
24+ 1
25+ / \
26+ 2 3
27+ / \ / \
28+ 4 5 6 7
29+ After calling your function, the tree should look like:
30+ 1 -> NULL
31+ / \
32+ 2 -> 3 -> NULL
33+ / \ / \
34+ 4->5->6->7 -> NULL
35+
36+ """
37+
38+ # Definition for binary tree with next pointer.
39+ # class TreeLinkNode(object):
340# def __init__(self, x):
441# self.val = x
542# self.left = None
643# self.right = None
744# self.next = None
845
9- class Solution :
10- # @param root, a tree node
11- # @return nothing
46+ class Solution (object ):
1247 def connect (self , root ):
48+ """
49+ :type root: TreeLinkNode
50+ :rtype: nothing
51+ """
1352 if root is not None :
1453 if root .left is not None :
1554 root .left .next = root .right
Original file line number Diff line number Diff line change 1+ """
2+ Remove all elements from a linked list of integers that have value val.
3+
4+ Example
5+ Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
6+ Return: 1 --> 2 --> 3 --> 4 --> 5
7+ """
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 removeElements (self , head , val ):
17+ """
18+ :type head: ListNode
19+ :type val: int
20+ :rtype: ListNode
21+ """
22+ if head is None :
23+ return None
24+ current = head
25+ last = None
26+ while current is not None :
27+ if current .val == val :
28+ if last is not None :
29+ # Remove `current` node and `last` node is not changed
30+ last .next = current .next
31+ else :
32+ # `current` is the head node
33+ # Remove the head node and `last` node is still None
34+ head = current .next
35+ last = None
36+ else :
37+ last = current
38+ current = current .next
39+ return head
You can’t perform that action at this time.
0 commit comments