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))
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))
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))
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))
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))
if not head or not head.next: return head count = 1 curr = head while curr.next is not None: count += 1 curr = curr.next curr.next = head curr, last, i = head, curr, 0 while i < (count - k % count): last = curr curr = curr.next i += 1 last.next = None return curr 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().rotate_list(h1, 89)) # print(Solution().is_palindrome_02(h2)) # print(Solution().is_palindrome(h2))