newCurrent.next = current newCurrent = newCurrent.next newCurrent.next = None current = next return newHead.next def printList(l: ListNode): while l: print(l.val, flush=True, sep=' ', end=' ') l = l.next print() l1 = ListNode(1) l1.next = ListNode(2) l1.next.next = ListNode(3) l1.next.next.next = ListNode(3) l1.next.next.next.next = ListNode(4) l1.next.next.next.next.next = ListNode(4) l1.next.next.next.next.next.next = ListNode(5) printList(Solution().deleteDuplicates(l1)) # 1 2 5 l2 = ListNode(1) l2.next = ListNode(1) l2.next.next = ListNode(1) l2.next.next.next = ListNode(2) l2.next.next.next.next = ListNode(3) printList(Solution().deleteDuplicates(l2)) # 2 3
Memory Usage: 15.3 MB, less than 62.20% of Python3 online submissions for Reverse Linked List. """ class Solution: def reverseList(self, head: ListNode) -> ListNode: curr = head prev: ListNode = None while curr: next = curr.next curr.next = prev prev = curr curr = next return prev def printList(l: ListNode): while l: print(l.val, flush=True, sep=' ', end=' ') l = l.next print() l = ListNode(1) l.next = ListNode(2) l.next.next = ListNode(3) l.next.next.next = ListNode(4) l.next.next.next.next = ListNode(5) printList(Solution().reverseList(l)) # 5 4 3 2 1
headA = advance(headA, cnt) if len2 > len1: cnt = len2 - len1 headB = advance(headB, cnt) while headA and headB: if headA == headB: return headA headA = headA.next headB = headB.next return None l1a = ListNode(4) l1a.next = ListNode(1) l1a.next.next = ListNode(8) l1a.next.next.next = ListNode(4) l1a.next.next.next.next = ListNode(5) l1b = ListNode(5) l1b.next = ListNode(6) l1b.next.next = ListNode(1) l1b.next.next.next = l1a.next.next print(Solution().getIntersectionNode(l1a, l1b).val) # 8 l2a = ListNode(1) l2a.next = ListNode(9) l2a.next.next = ListNode(1) l2a.next.next.next = ListNode(2) l2a.next.next.next.next = ListNode(4)
current.next = tail next.next = current if prev: prev.next = next prev = current current = tail return newHead def printList(l: ListNode): while l: print(l.val, flush=True, sep=' ', end=' ') l = l.next print() list1 = ListNode(1) list1.next = ListNode(2) list1.next.next = ListNode(3) list1.next.next.next = ListNode(4) list1.next.next.next.next = ListNode(5) printList(Solution().swapPairs(list1)) list2 = ListNode(1) list2.next = ListNode(2) list2.next.next = ListNode(3) list2.next.next.next = ListNode(4) printList(Solution().swapPairs(list2))
curr: ListNode = head while curr: next = curr.next curr.next = prev prev = curr curr = next return prev def isPalindrome(self, head: ListNode) -> bool: if head is None: return True middle = self.getMiddle(head) middle = self.reverse(middle) while middle: if middle.val != head.val: return False middle = middle.next head = head.next return True l1 = ListNode(1) l1.next = ListNode(2) print(Solution().isPalindrome(l1)) # False l2 = ListNode(1) l2.next = ListNode(2) l2.next.next = ListNode(2) l2.next.next.next = ListNode(1) print(Solution().isPalindrome(l2)) # True
prev = curr curr = curr.next if prev2: prev2.next = None def printList(l: ListNode): while l: print(l.val, flush=True, sep=' ', end=' ') l = l.next print() l1 = ListNode(4) n5 = l1.next = ListNode(5) l1.next.next = ListNode(1) l1.next.next.next = ListNode(9) Solution().deleteNode(n5) printList(l1) # 4 1 9 l2 = ListNode(4) l2.next = ListNode(5) n1 = l2.next.next = ListNode(1) l2.next.next.next = ListNode(9) Solution().deleteNode(n1) printList(l2) # 4 5 9