Skip to content

Commit a7aaa85

Browse files
committed
added learn-cards problems
1 parent bf938b3 commit a7aaa85

13 files changed

+346
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
class Node:
2+
def __init__(self, val):
3+
self.val = val
4+
self.next = None
5+
6+
class MyLinkedList:
7+
8+
def __init__(self):
9+
"""
10+
Initialize your data structure here.
11+
"""
12+
self.head = None
13+
14+
def get(self, index: int) -> int:
15+
"""
16+
Get the value of the index-th node in the linked list. If the index is invalid, return -1.
17+
"""
18+
cur = self.getNodeAtIndex(index)
19+
if cur is not None:
20+
return cur.val
21+
else:
22+
return -1
23+
24+
def addAtHead(self, val: int) -> None:
25+
"""
26+
Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
27+
"""
28+
new_head = Node(val)
29+
new_head.next = self.head
30+
self.head = new_head
31+
return
32+
33+
def addAtTail(self, val: int) -> None:
34+
"""
35+
Append a node of value val to the last element of the linked list.
36+
"""
37+
if self.head is None:
38+
self.addAtHead(val)
39+
return
40+
41+
new_node = Node(val)
42+
old_tail = self.getTail()
43+
old_tail.next = new_node
44+
45+
def addAtIndex(self, index: int, val: int) -> None:
46+
"""
47+
Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
48+
"""
49+
if index == 0:
50+
self.addAtHead(val)
51+
return
52+
53+
prev = self.getNodeAtIndex(index - 1)
54+
if prev is None:
55+
return
56+
57+
new_node = Node(val)
58+
next_node = prev.next
59+
new_node.next = next_node
60+
prev.next = new_node
61+
62+
def deleteAtIndex(self, index: int) -> None:
63+
"""
64+
Delete the index-th node in the linked list, if the index is valid.
65+
"""
66+
cur = self.getNodeAtIndex(index)
67+
if cur is None:
68+
return
69+
70+
prev = self.getNodeAtIndex(index - 1)
71+
next_node = cur.next
72+
73+
if prev is not None:
74+
prev.next = next_node
75+
else:
76+
self.head = next_node
77+
78+
def getNodeAtIndex(self, index: int) -> 'Node':
79+
if index < 0:
80+
return None
81+
82+
cur = self.head
83+
i = 0
84+
while i < index and cur is not None:
85+
cur = cur.next
86+
i += 1
87+
return cur
88+
89+
def getTail(self) -> 'Node':
90+
cur = self.head
91+
while cur is not None and cur.next is not None:
92+
cur = cur.next
93+
return cur
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def hasCycle(self, head: ListNode) -> bool:
3+
slow = head
4+
fast = head
5+
6+
while fast and fast.next:
7+
slow = slow.next
8+
fast = fast.next.next
9+
10+
if slow == fast:
11+
return True
12+
13+
return False
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def detectCycle(self, head: ListNode) -> ListNode:
3+
slow = head
4+
fast = head
5+
6+
while fast and fast.next:
7+
slow = slow.next
8+
fast = fast.next.next
9+
10+
if slow == fast:
11+
break
12+
13+
if not fast or not fast.next:
14+
return None
15+
16+
slow = head
17+
while slow != fast:
18+
slow = slow.next
19+
fast = fast.next
20+
21+
return slow
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
3+
p1 = headA
4+
p2 = headB
5+
6+
while p1 != p2:
7+
p1 = headB if p1 is None else p1.next
8+
p2 = headA if p2 is None else p2.next
9+
10+
return p1
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
3+
slow = head
4+
fast = head
5+
6+
for _ in range(n):
7+
fast = fast.next
8+
9+
if not fast:
10+
return head.next
11+
12+
while fast.next:
13+
slow = slow.next
14+
fast = fast.next
15+
16+
slow.next = slow.next.next
17+
return head
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def reverseList(self, head: ListNode) -> ListNode:
3+
if not head or not head.next:
4+
return head
5+
6+
reversedHead = self.reverseList(head.next)
7+
head.next.next = head
8+
head.next = None
9+
10+
return reversedHead
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def removeElements(self, head: ListNode, val: int) -> ListNode:
3+
dummy = ListNode()
4+
dummy.next = head
5+
prev = dummy
6+
cur = head
7+
8+
while cur:
9+
if cur.val == val:
10+
prev.next = cur.next
11+
else:
12+
prev = cur
13+
14+
cur = cur.next
15+
16+
return dummy.next
17+
18+
class Solution2:
19+
def removeElements(self, head: ListNode, val: int) -> ListNode:
20+
if not head: return None
21+
22+
head.next = self.removeElements(head.next, val)
23+
24+
if head.val == val:
25+
return head.next
26+
27+
return head
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def oddEvenList(self, head: ListNode) -> ListNode:
3+
if not head or not head.next: return head
4+
5+
odd_head = head
6+
even_head = head.next
7+
odd = odd_head
8+
even = even_head
9+
10+
while even:
11+
if even.next:
12+
odd.next = even.next
13+
else:
14+
odd.next = even_head
15+
return odd_head
16+
17+
odd.next = even.next
18+
odd = odd.next
19+
even.next = odd.next
20+
even = even.next
21+
22+
odd.next = even_head
23+
return odd_head
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Solution:
2+
def isPalindrome(self, head: ListNode) -> bool:
3+
self.left = head
4+
5+
def isPalindromeRec(node):
6+
if node is not None:
7+
if not isPalindromeRec(node.next):
8+
return False
9+
if self.left.val != node.val:
10+
return False
11+
self.left = self.left.next
12+
13+
return True
14+
15+
return isPalindromeRec(head)
16+
17+
class Solution2:
18+
def isPalindrome(self, head: ListNode) -> bool:
19+
if not head: return True
20+
21+
mid = self.findMid(head)
22+
second_half_start = self.reverseList(mid.next)
23+
24+
res = True
25+
left = head
26+
right = second_half_start
27+
while res and right:
28+
if left.val != right.val:
29+
res = False
30+
left = left.next
31+
right = right.next
32+
33+
mid.next = self.reverseList(second_half_start)
34+
return res
35+
36+
def findMid(self, head):
37+
slow = head
38+
fast = head
39+
while fast.next and fast.next.next:
40+
slow = slow.next
41+
fast = fast.next.next
42+
return slow
43+
44+
def reverseList(self, head):
45+
prev = None
46+
cur = head
47+
while cur:
48+
next_node = cur.next
49+
cur.next = prev
50+
prev = cur
51+
cur = next_node
52+
return prev

leetcode/learn-cards/linked-list/10_design_doubly_linked_list.py

Whitespace-only changes.

0 commit comments

Comments
 (0)