def swapPairs(self, head): # write your code here if not head or head.next is None: return head dummyNode = ListNode(0) dummyNode.next = head while head.next is not None: tmp = head.next head.next = head.next.next tmp.next = head head = head.next return dummyNode.next
def removeNthFromEnd(self, head, n): # write your code here cur = head dummy = ListNode(0) dummy.next = head pre = dummy for i in range(1, n + 1): head = head.next while head is not None: head = head.next cur = cur.next pre = pre.next pre.next = cur.next return dummy.next
def swapPairs2(self, head): dummy = ListNode(0) left = dummy dummy.next = head mid = head right = head.next while right.next is not None: mid.next = right.next right.next = mid left.next = right left = left.next.next mid = mid.next right = mid.next return dummy.next
def findMid(self, head): if not head: return None if head.next is None: return head if head.next.next is None: return head dummy = ListNode(0) dummy.next = head pre = dummy cur = head while head is not None and head.next is not None: head = head.next.next cur = cur.next pre = pre.next if head is None: return pre return cur
def deleteDuplicates2(self, head): if head == None: return None dummyNode = ListNode(0) dummyNode.next = head pre = dummyNode while head != None: if head.next != None and head.val == head.next.val: while head.next != None and head.val == head.next.val: head = head.next pre.next = head.next head = head.next else: pre = pre.next # or pre=head head = head.next return dummyNode.next
def removeElements(self, head, val): # write your code here if head == None: return None dummyNode = ListNode(0) dummyNode.next = head pre = dummyNode # if head.val == val: return dummyNode.next.next while head is not None: if head.val == val: pre.next = head.next head = head.next continue pre = pre.next head = head.next return dummyNode.next
def findmid(head): if not head: return None if head.next is None: return head if head.next.next is None: return head dummy = ListNode(0) dummy.next = head pre = dummy cur = head while head is not None and head.next is not None: head = head.next.next cur = cur.next pre = pre.next if head is None: return pre # enev case return cur # ods case
def deleteDuplicates(self, head): # write your code here if head == None: return None dummyNode = ListNode(0) dummyNode.next = head pre = dummyNode while head != None: if head.next != None and head.val == head.next.val: value = head.val # todo important while head != None and head.val == value: head = head.next # todo also pre.next = head else: pre = head head = head.next return dummyNode.next
def partition(self, head, x): # write your code here if not head: return 0 Leftdummy = ListNode(0) rightdummy = ListNode(0) left = Leftdummy right = rightdummy while head is not None: if head.val < x: left.next = head left = left.next else: right.next = head right = right.next head = head.next right.next = None left.next = rightdummy.next return Leftdummy.next
def rotateRight(self, head, k): # write your code here if not head: return None if head.next is None: return head cur = head dummy = ListNode(0) dummy.next = head pre = head j = 0 for i in range(1, k + 1): if head.next is not None: j += 1 head = head.next if j <= k: return pre while head.next is not None: head = head.next cur = cur.next # pre = pre.next cur.next = None head.next = pre return head
def merge(self, start, mid): i = start j = mid dummy = ListNode(0) dummy.next = start while not start and not mid: if start.val < mid.val: dummy.next = start start = start.next else: dummy.next = mid mid = mid.next dummy.next = dummy return dummy.next
return dummyNode.next def swap(self, head, pre): if head.next.next is not None: head.next = head.next.next head.next.next = pre pre = head self.swap(head.next.next, pre) # https://mp.weixin.qq.com/s?__biz=MzU0OTU5OTI4MA==&idx=2&mid=2247485084&sn=78dfedad9f8e6840378a60b6c1f0ae4f # https://leetcode-cn.com/problems/swap-nodes-in-pairs/solution/hua-jie-suan-fa-24-liang-liang-jiao-huan-lian-biao/ def swapPairs2(self, head): dummy = ListNode(0) left = dummy dummy.next = head mid = head right = head.next while right.next is not None: mid.next = right.next right.next = mid left.next = right left = left.next.next mid = mid.next right = mid.next return dummy.next s = Solution() s.swapPairs2(head_with_6ele_repeat4end) ListNode.printLinklist(head_with_6ele_repeat4end)
dummy.next = start while not start and not mid: if start.val < mid.val: dummy.next = start start = start.next else: dummy.next = mid mid = mid.next dummy.next = dummy return dummy.next def findMid(self, head): if not head: return None if head.next is None: return head if head.next.next is None: return head dummy = ListNode(0) dummy.next = head pre = dummy cur = head while head is not None and head.next is not None: head = head.next.next cur = cur.next pre = pre.next if head is None: return pre return cur s = Solution() a = s.sortList(head_with_6ele_repeat4end) ListNode.printLinklist(a)