Skip to content

Commit 940a000

Browse files
committed
[Feat] Add some implementation on LinkedList
1 parent 9dcbf4f commit 940a000

9 files changed

+176
-0
lines changed

002-AddTwoNumbers.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def addTwoNumbers(
8+
self, l1: Optional[ListNode], l2: Optional[ListNode]
9+
) -> Optional[ListNode]:
10+
# Reverse int L1
11+
val1 = ""
12+
curr1 = l1
13+
while curr1:
14+
temp = val1
15+
val1 = str(curr1.val) + temp
16+
curr1 = curr1.next
17+
18+
# Reverse int L2
19+
val2 = ""
20+
curr2 = l2
21+
while curr2:
22+
temp = val2
23+
val2 = str(curr2.val) + temp
24+
curr2 = curr2.next
25+
26+
# jumlahkan int L1 + int L2 dan buat listNode baru
27+
result = str(int(val1) + int(val2))
28+
length = len(result)
29+
newList = ListNode()
30+
31+
temp = newList
32+
while length > 0:
33+
temp.next = ListNode(int(result[length - 1]), None)
34+
temp = temp.next
35+
length -= 1
36+
37+
return newList.next

019-RemoveNthNodeFromEndOfList.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
8+
newNode = ListNode(0, head)
9+
left = newNode
10+
right = head
11+
12+
for i in range(n):
13+
right = right.next
14+
15+
while right:
16+
right = right.next
17+
left = left.next
18+
19+
left.next = left.next.next
20+
return newNode.next

021-MergeTwoSortedLists.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def mergeTwoLists(
8+
self, list1: Optional[ListNode], list2: Optional[ListNode]
9+
) -> Optional[ListNode]:
10+
dummy = ListNode()
11+
current = dummy
12+
13+
while list1 and list2:
14+
if list1.val <= list2.val:
15+
current.next = list1
16+
list1 = list1.next
17+
else:
18+
current.next = list2
19+
list2 = list2.next
20+
current = current.next
21+
22+
if list1:
23+
current.next = list1
24+
elif list2:
25+
current.next = list2
26+
27+
return dummy.next

055-JumpGame.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def canJump(self, nums: List[int]) -> bool:
3+
goal = len(nums) - 1
4+
5+
for i in range(len(nums) - 1, -1, -1):
6+
if i + nums[i] >= goal:
7+
goal = i
8+
9+
if goal == 0:
10+
return True
11+
else:
12+
return False

141-LinkedListCycle.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 hasCycle(self, head: Optional[ListNode]) -> bool:
9+
slow, fast = head, head
10+
11+
while fast and fast.next:
12+
slow = slow.next
13+
fast = fast.next.next
14+
15+
if slow == fast:
16+
return True
17+
18+
return False
19+

143-ReorderList.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def reorderList(self, head: Optional[ListNode]) -> None:
8+
"""
9+
Do not return anything, modify head in-place instead.
10+
"""
11+
# Cari titik tengah
12+
slow, fast = head, head.next
13+
while fast and fast.next:
14+
slow = slow.next
15+
fast = fast.next.next
16+
17+
# Tangkap dan pecahkan linkedlist jadi 2
18+
second = slow.next
19+
prev, slow.next = None, None
20+
21+
# Reverse linkedlist second
22+
while second:
23+
temp = second.next
24+
second.next = prev
25+
prev = second
26+
second = temp
27+
28+
# Merge Linkedlist
29+
list1, list2 = head, prev
30+
while list2:
31+
temp1, temp2 = list1.next, list2.next
32+
list1.next = list2
33+
list2.next = temp1
34+
list1, list2 = temp1, temp2
35+
return list1

206-ReverseLinkedList.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
7+
# Iterative Approach
8+
class Solution:
9+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
10+
prev, curr = None, head
11+
12+
while curr:
13+
temp = curr.next
14+
curr.next = prev
15+
prev = curr
16+
curr = temp
17+
18+
return prev
19+

287-FindTheDuplicateNumber.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def findDuplicate(self, nums: List[int]) -> int:
3+
valHistory = set()
4+
for num in nums:
5+
if num in valHistory:
6+
return num
7+
valHistory.add(num)
File renamed without changes.

0 commit comments

Comments
 (0)