def reverse_list(self, head, m, n): diff, dummy, cur = n - m + 1, Node(-1), head dummy.next = head last_unswapped = dummy while cur and m > 1: cur, last_unswapped, m = cur.next, cur, m - 1 prev, first_swapped = last_unswapped, cur while cur and diff > 0: cur.next, prev, cur, diff = prev, cur, cur.next, diff - 1 last_unswapped.next, first_swapped.next = prev, cur return dummy.next
def remove_duplicates(self, head): temp = Node(0) prev, curr = temp, head while curr: if curr.next and curr.next.val == curr.val: val = curr.val while curr and curr.val == val: curr = curr.next prev.next = curr else: prev.next = curr prev = curr curr = curr.next return temp.next
from Python.Level4.LinkedList import Node, Traverse class Solution: # @param A : head node of linked list # @return the head node in the linked list def delete_duplicates(self, head): if not head: return head ptr1 = head ptr2 = head.next while ptr2: if ptr1.val == ptr2.val: # Remove the node ptr1.next = ptr2.next else: ptr1 = ptr1.next ptr2 = ptr2.next return head """Testing Code """ if __name__ == "__main__": # initializing the linked list values head, head.next, head.next.next = Node(1), Node(1), Node(2) head.next.next.next, head.next.next.next.next = Node(3), Node(3) # printing the new list Traverse().print_list(Solution().delete_duplicates(head))
from Python.Level4.LinkedList import Node, Traverse class Solution: def substract(self, head): curr, count, i = head, 0, 0 temp = head temp_arr = [] while temp is not None: temp_arr.append(temp.val) temp = temp.next count += 1 while i < count // 2: curr.val = temp_arr.pop() - curr.val curr = curr.next i += 1 return head if __name__ == "__main__": # initializing the linked list values # h1, h1.next, h1.next.next, h1.next.next.next, h1.next.next.next.next, h1.next.next.next.next.next = Node(1), Node( # 2), Node(3), Node(4), Node(5), Node(6) h1, h1.next, h1.next.next = Node(1), Node(3), Node(4) Traverse().print_list(Solution().substract(h1))
if prev is None: head = curr_ptr.next head.next = curr_ptr.next.next return head prev = prev.next count += 1 if count == length: head = curr_ptr.next return head elif count == 0: while curr_ptr.next is not None: prev = curr_ptr curr_ptr = curr_ptr.next prev.next = None return head else: while prev.next is not None: curr_ptr = curr_ptr.next prev = prev.next curr_ptr.next = curr_ptr.next.next return head if __name__ == "__main__": # initializing the linked list values h1, h1.next, h1.next.next, h1.next.next.next = Node(1), Node(2), Node( 3), Node(4) # h2, h2.next, h2.next.next, h2.next.next.next = Node(4), Node(5), Node(6), Node(7) # printing the new list Traverse().print_list(Solution().remove_nth_node(h1, 6))
def is_palindrome_03(self, head): reverse, fast = None, head # Reverse the first half part of the list. while fast and fast.next: fast = fast.next.next head.next, reverse, head = reverse, head, head.next # If the number of the nodes is odd, # set the head of the tail list to the next of the median node. tail = head.next if fast else head # Compare the reversed first half list with the second half list. # And restore the reversed first half list. is_palindrome = 1 while reverse: is_palindrome = is_palindrome and reverse.val == tail.val reverse.next, head, reverse = head, reverse, reverse.next tail = tail.next return is_palindrome if __name__ == "__main__": # initializing the linked list values # h1, h1.next, h1.next.next, h1.next.next.next, h1.next.next.next.next = Node(1), Node(4), Node(6), Node(4), Node(1) h2, h2.next, h2.next.next = Node(1), Node(2), Node(1) # printing the new list # Traverse().print_list(Solution().is_palindrome(h1)) print(Solution().is_palindrome_02(h2)) # print(Solution().is_palindrome(h2))
class Solution: def remove_duplicates(self, head): temp = Node(0) prev, curr = temp, head while curr: if curr.next and curr.next.val == curr.val: val = curr.val while curr and curr.val == val: curr = curr.next prev.next = curr else: prev.next = curr prev = curr curr = curr.next return temp.next """Testing Code """ if __name__ == "__main__": # initializing the linked list values head, head.next, head.next.next = Node(1), Node(1), Node(3) head.next.next.next, head.next.next.next.next = Node(4), Node(4) head.next.next.next.next.next, head.next.next.next.next.next.next = Node( 4), Node(4) # printing the new list Traverse().print_list(Solution().remove_duplicates(head))
class Solution: def reverse_list(self, head, m, n): diff, dummy, cur = n - m + 1, Node(-1), head dummy.next = head last_unswapped = dummy while cur and m > 1: cur, last_unswapped, m = cur.next, cur, m - 1 prev, first_swapped = last_unswapped, cur while cur and diff > 0: cur.next, prev, cur, diff = prev, cur, cur.next, diff - 1 last_unswapped.next, first_swapped.next = prev, cur return dummy.next if __name__ == "__main__": # initializing the linked list values h1, h1.next, h1.next.next, h1.next.next.next, h1.next.next.next.next, h1.next.next.next.next.next = Node( 1), Node(2), Node(3), Node(4), Node(5), Node(6) # h1, h1.next, h1.next.next, h1.next.next.next, h1.next.next.next.next = Node(1), Node(2), Node(3), Node(4), Node(5) # h1, h1.next, h1.next.next = Node(1), Node(2), Node(3) # h2, h2.next, h2.next.next = Node(1), Node(2), Node(1) # printing the new list Traverse().print_list(Solution().reverse_list(h1, 2, 4))
def merge(self, h1, h2): p1 = h1 p2 = h2 new_head = None while (p1 is not None) and (p2 is not None): if p1.val < p2.val: e = Node(p1.val) e.next = new_head new_head = e p1 = p1.next else: e = Node(p2.val) e.next = new_head new_head = e p2 = p2.next while p1 is not None: e = Node(p1.val) e.next = new_head new_head, p1 = e, p1.next while p2 is not None: e = Node(p2.val) e.next = new_head new_head, p2 = e, p2.next # Reversing the list prev = None current = new_head while current is not None: nx = current.next current.next = prev prev = current current = nx new_head = prev return new_head
e.next = new_head new_head, p1 = e, p1.next while p2 is not None: e = Node(p2.val) e.next = new_head new_head, p2 = e, p2.next # Reversing the list prev = None current = new_head while current is not None: nx = current.next current.next = prev prev = current current = nx new_head = prev return new_head """Testing Code """ if __name__ == "__main__": # initializing the linked list values h1, h1.next, h1.next.next = Node(1), Node(2), Node(3) h2, h2.next, h2.next.next, h2.next.next.next = Node(4), Node(5), Node( 6), Node(7) # printing the new list Traverse().print_list(Solution().merge(h1, h2))
while ptr1 is not None: ptr2 = new_head while ptr2.next is not None: ptr3 = ptr2 ptr2 = ptr2.next ptr4 = ptr1.next ptr1.next = ptr2 ptr2.next = ptr4 if ptr3 is not None: ptr3.next = None ptr1 = ptr4 res = head if count % 2 != 0: while res.next is not None: res = res.next res.next = ptr3 return head if __name__ == "__main__": # initializing the linked list values # h1, h1.next, h1.next.next, h1.next.next.next, h1.next.next.next.next, h1.next.next.next.next.next = Node(1), Node( # 2), Node(3), Node(4), Node(5), Node(6) # h1, h1.next, h1.next.next, h1.next.next.next, h1.next.next.next.next = Node(1), Node(2), Node(3), Node(4), Node(5) h1, h1.next,h1.next.next = Node(1), Node(2), Node(3) # h2, h2.next, h2.next.next = Node(1), Node(2), Node(1) # printing the new list Traverse().print_list(Solution().reorder_list(h1)) # print(Solution().is_palindrome_02(h2)) # print(Solution().is_palindrome(h2))