Skip to content

Commit

Permalink
Linked List
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqueimaginate committed Feb 24, 2021
1 parent 05e5eb7 commit dc39c5f
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 4 deletions.
62 changes: 62 additions & 0 deletions Coding Test/2.py
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
22 changes: 22 additions & 0 deletions Coding Test/206.py
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
14 changes: 14 additions & 0 deletions Coding Test/21.py
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

42 changes: 42 additions & 0 deletions Coding Test/234.py
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
44 changes: 44 additions & 0 deletions Coding Test/24.py
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
52 changes: 52 additions & 0 deletions Coding Test/328.py
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
23 changes: 23 additions & 0 deletions Coding Test/92.py
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
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@

1. Array
> 1, 42, 15, 561, 238, 121
2. Stack
3. Queue
4. Deque
2. Linked List
> 234, 21, 206, 2, 24, 328, 92
3. Stack, Queue
4. Deque, Priority Queue
5. Tree, BST, Trie, Segment Tree
6. Heap, Priority Queue
6. Heap
7. Graph
8. Hash Table

Expand Down

0 comments on commit dc39c5f

Please sign in to comment.