def partition(self, head: ListNode, x: int) -> ListNode: if not head: return None dummy = ListNode(0) dummy.next = head before = None pre = None current = head while current: if current.val < x and pre and pre.val >= x: if not before: before = dummy nxt = current.next pre.next = nxt current.next = before.next before.next = current before = current current = nxt else: if current.val < x: before = current pre = current current = current.next return dummy.next
def reverseList(self, head: ListNode) -> ListNode: dummy = ListNode(None) dummy.next = head while head and head.next: nxt = head.next head.next = nxt.next nxt.next = dummy.next dummy.next = nxt return dummy.next
def deleteDuplicates(self, head: ListNode) -> ListNode: root = ListNode(0) root.next = head pre = root while head and head.next: if head.next and head.val != head.next.val: pre = head head = head.next else: while head.next and head.val == head.next.val: head = head.next head = head.next pre.next = head return root.next
def removeDuplicateNodes(self, head: ListNode) -> ListNode: nodes = set() root = ListNode(None) root.next = head pre = root while head: if head.val in nodes: head = head.next pre.next = head else: nodes.add(head.val) head = head.next pre = pre.next return root.next
def removeElements(self, head: ListNode, val: int) -> ListNode: """ 通常链表操作都会添加一个哨兵节点,能显著降低边界条件判断的复杂度 """ dummy = ListNode(None) dummy.next = head pre = dummy current = head while current: if current.val == val: pre.next = current.next else: pre = pre.next current = current.next return dummy.next
def addAtHead(self, val: int) -> None: """ Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. """ bef_head = self.dummy.next self.dummy.next = ListNode(val) self.dummy.next.next = bef_head
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: """ 双指针解法 """ root = ListNode(0) head = root jinwei = 0 while l1 and l2: val = l1.val + l2.val + jinwei if val > 9: jinwei = 1 val -= 10 else: jinwei = 0 head.next = ListNode(val) head = head.next l1 = l1.next l2 = l2.next while l1: val = l1.val + jinwei if val > 9: jinwei = 1 val -= 10 else: jinwei = 0 head.next = ListNode(val) head = head.next l1 = l1.next while l2: val = l2.val + jinwei if val > 9: jinwei = 1 val -= 10 else: jinwei = 0 head.next = ListNode(val) head = head.next l2 = l2.next if jinwei > 0: head.next = ListNode(jinwei) return root.next
def addAtIndex(self, index: int, val: int) -> None: """ Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. """ pre = self.dummy current = self.dummy.next counter = 0 while current: if counter == index: pre.next = ListNode(val) pre.next.next = current break current = current.next pre = pre.next counter += 1 if not current and counter==index: pre.next=ListNode(val)
def addAtTail(self, val: int) -> None: """ Append a node of value val to the last element of the linked list. """ pre=self.dummy current = self.dummy.next while current: current = current.next pre=pre.next pre.next = ListNode(val)
def partition(self, head: ListNode, x: int) -> ListNode: """ 如果题目中没明确说明在原链表上操作,可以打散成单个节点,再组装成链表。这样和人的思维方式一致,简单易处理。 """ # 添加哨兵节点,方便处理 less_dummy = ListNode(None) less_current = less_dummy great_dummy = ListNode(None) great_current = great_dummy while head: if head.val < x: less_current.next = head less_current = less_current.next else: great_current.next = head great_current = great_current.next head = head.next great_current.next = None less_current.next = great_dummy.next return less_dummy.next
def reverseList(self, head: ListNode) -> ListNode: dummy = ListNode(None) stack = [] while head: stack.append(head) head = head.next current = dummy while stack: current.next = stack.pop() current = current.next current.next = None return dummy.next
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: """双指针解法,代码好看版本""" head = ListNode(0) node = head remaining = 0 while l1 or l2: if l1 == None: node.next = l2 l1 = ListNode(0) if l2 == None: node.next = l1 l2 = ListNode(0) remaining += l1.val + l2.val node.next = ListNode(remaining % 10) remaining = remaining // 10 node = node.next l1 = l1.next l2 = l2.next if remaining: node.next = ListNode(remaining) return head.next
def partition(self, head: ListNode, x: int) -> ListNode: """ 方法一,暴力法 """ root1, root2, root3 = ListNode(0), ListNode(0), ListNode(0) head1, head2, head3 = root1, root2, root3 while head: if head.val < x: head1.next = head head1 = head1.next elif head.val > x: head2.next = head head2 = head2.next elif head.val == x: head3.next = head head3 = head3.next head = head.next if root2.next: head1.next = root2.next head2.next = root3.next else: head1.next = root3.next head3.next = None return root1.next if root1.next else root3.next
def __init__(self): """ Initialize your data structure here. """ self.dummy = ListNode(None)