-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05e5eb7
commit dc39c5f
Showing
8 changed files
with
264 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Definition for singly-linked list. | ||
class ListNode: | ||
def __init__(self, val=0, next=None): | ||
self.val = val | ||
self.next = next | ||
|
||
|
||
class Solution: | ||
def reverseList(self, head: ListNode) -> ListNode: | ||
node, prev = head, None | ||
|
||
while node: | ||
next, node.next = node.next, prev | ||
prev, node = node, next | ||
return prev | ||
|
||
def toList(self, node: ListNode) -> List: | ||
list: List = [] | ||
while node: | ||
list.append(node.val) | ||
node = node.next | ||
return list | ||
|
||
def toReversedLinkedList(self, result: str) -> ListNode: | ||
prev: ListNode = None | ||
for r in result: | ||
node = ListNode(r) | ||
node.next = prev | ||
prev = node | ||
return node | ||
|
||
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: | ||
a = self.toList(self.reverseList(l1)) | ||
b = self.toList(self.reverseList(l2)) | ||
|
||
resultStr = int(''.join(map(lambda x: str(x), a))) + \ | ||
int(''.join(map(lambda x: str(x), b))) | ||
|
||
return self.toReversedLinkedList(str(resultStr)) | ||
|
||
|
||
class Solution2: | ||
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: | ||
root = head = ListNode(0) | ||
|
||
carry = 0 | ||
while l1 or l2 or carry: | ||
sum = 0 | ||
if l1: | ||
sum += l1.val | ||
l1 = l1.next | ||
|
||
if l2: | ||
sum += l2.val | ||
l2 = l2.next | ||
|
||
carry, val = divmod(sum + carry, 10) | ||
|
||
head.next = ListNode(val) | ||
head = head.next | ||
|
||
return root.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
class Solution: | ||
def reverseList(self, head: ListNode) -> ListNode: | ||
prev, node = None, head | ||
while node: | ||
prev, prev.next, node = node, prev, node.next | ||
|
||
return prev | ||
|
||
|
||
class Solution2: | ||
def reverseList(self, head: ListNode) -> ListNode: | ||
prev, node = None, head | ||
while node: | ||
next, node.next = node.next, prev | ||
prev, node = node, next | ||
|
||
return prev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
class Solution: | ||
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: | ||
if (not l1) or (l2 and l1.val > l2.val): | ||
l1, l2 = l2, l1 | ||
|
||
if l1: | ||
l1.next = self.mergeTwoLists(l1.next, l2) | ||
return l1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import re | ||
import collections | ||
|
||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
|
||
# Using deque | ||
class Solution: | ||
def isPalindrome(self, head: ListNode) -> bool: | ||
d = collections.deque() | ||
curr_node = head | ||
while curr_node: | ||
d.append(curr_node.val) | ||
if(curr_node.next): | ||
curr_node = curr_node.next | ||
else: | ||
break | ||
|
||
while len(d) > 1: | ||
if d.popleft() != d.pop(): | ||
return False | ||
|
||
return True | ||
|
||
# Elegant | ||
class Solution2: | ||
def isPalindrome(self, head: ListNode) -> bool: | ||
rev = None | ||
slow = fast = head | ||
|
||
while fast and fast.next: | ||
fast = fast.next.next | ||
rev, rev.next, slow = slow, rev, slow.next | ||
if fast: | ||
slow = slow.next | ||
|
||
while rev and rev.val == slow.val: | ||
slow, rev = slow.next, rev.next | ||
return not rev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
|
||
# Swap the value not the node | ||
class Solution: | ||
def swapPairs(self, head: ListNode) -> ListNode: | ||
node = head | ||
|
||
while node and node.next: | ||
node.val, node.next.val = node.next.val, node.val | ||
node = node.next.next | ||
|
||
return head | ||
|
||
# Recursive | ||
|
||
|
||
class Solution2: | ||
def swapPairs(self, head: ListNode) -> ListNode: | ||
|
||
if not head or not head.next: | ||
return head | ||
|
||
first_node = head | ||
second_node = head.next | ||
|
||
first_node.next = self.swapPairs(second_node.next) | ||
second_node.next = first_node | ||
|
||
return second_node | ||
|
||
# Iterative | ||
class Solution3: | ||
def swapPairs(self, head): | ||
pre, pre.next = self, head | ||
while pre.next and pre.next.next: | ||
a = pre.next | ||
b = a.next | ||
pre.next, b.next, a.next = b, a, b.next | ||
pre = a | ||
return self.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
|
||
# My first Solution. looks bad | ||
class Solution: | ||
def oddEvenList(self, head: ListNode) -> ListNode: | ||
if not head or not head.next: | ||
return head | ||
|
||
prev = head | ||
head = head.next | ||
first = prev | ||
second = head | ||
count = 0 | ||
while head: | ||
count += 1 | ||
if head.next: | ||
prev.next = head.next | ||
prev = head | ||
head = head.next | ||
else: | ||
prev.next = None | ||
break | ||
|
||
if count % 2 == 1: | ||
prev.next = second | ||
else: | ||
head.next = second | ||
|
||
return first | ||
|
||
|
||
# My second Solution | ||
class Solution2: | ||
def oddEvenList(self, head: ListNode) -> ListNode: | ||
if not head: | ||
return head | ||
|
||
odd = head | ||
even = head.next | ||
even_head = head.next | ||
|
||
while even and even.next: | ||
odd.next, even.next = odd.next.next, even.next.next | ||
odd, even = odd.next, even.next | ||
|
||
odd.next = even_head | ||
|
||
return head |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
class Solution: | ||
def reverseBetween(self, head: ListNode, left: int, right: int) -> ListNode: | ||
if not head or left==right: | ||
return head | ||
|
||
root = start = ListNode(None) | ||
root.next = head | ||
|
||
for _ in range(left - 1): | ||
start = start.next | ||
|
||
end = start.next | ||
|
||
for _ in range(right - left): | ||
tmp, start.next, end.next = start.next, end.next, end.next.next | ||
start.next.next = tmp | ||
|
||
return root.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters