def sortList(self, head: ListNode) -> ListNode: if not head or not head.next: return head if not head.next.next: if head.val > head.next.val: nxt = head.next head.next.next = head head.next = None return nxt return head rtn = ListNode() rtn.next = head slow = fast = rtn while fast and fast.next: slow = slow.next fast = fast.next.next right = self.sortList(slow.next) slow.next = None left = self.sortList(head) rtn = tmp = ListNode() while left and right: if left.val <= right.val: tmp.next = left left = left.next else: tmp.next = right right = right.next tmp = tmp.next tmp.next = left if left else right return rtn.next
def reverseBetween(self, head: ListNode, left: int, right: int) -> ListNode: if left == right: return head rtn = ListNode() rtn.next = head start = rtn cnt = 1 while head and cnt != left: start = head head = head.next cnt += 1 pre = None while head and cnt != right: nxt = head.next head.next = pre pre = head head = nxt cnt += 1 if head: start.next.next = head.next head.next = pre start.next = head return rtn.next
def swapPairs(self, head: ListNode) -> ListNode: cur = ListNode() rtn = cur while head and head.next: new_head = head.next.next cur.next = head.next head.next.next = head head.next = None cur = head head = new_head if head: cur.next = head return rtn.next
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: # if not l1 or not l2: # return l1 if l1 else l2 dummy = ListNode(0) rtn = dummy while l1 and l2: if l1.val <= l2.val: dummy.next = l1 l1 = l1.next else: dummy.next = l2 l2 = l2.next dummy = dummy.next dummy.next = l1 if l1 else l2 return rtn.next
def mergeTwoListsRecursion(self, l1: ListNode, l2: ListNode) -> ListNode: """ 递归解法 :param l1: :param l2: :return: """ if l1 is None: return l2 if l2 is None: return l1 if l1.val <= l2.val: l1.next = self.mergeTwoLists(l1.next, l2) return l1 else: l2.next = self.mergeTwoLists(l1, l2.next) return l2
def deleteDuplicates(self, head: ListNode) -> ListNode: rtn = head while head: if head.next and head.val == head.next.val: nxt = head.next head.next = head.next.next del nxt else: head = head.next return rtn
def reverseList(self, head: ListNode) -> ListNode: if not head: return head pre, cur = head, head.next head.next = None while cur: nxt = cur.next cur.next = pre cur, pre = nxt, cur return pre
def removeElements(self, head, val): """ :type head: ListNode :type val: int :rtype: ListNode """ phead = ListNode(0) phead.next = head p = phead while p: while p.next and p.next.val == val: p.next = p.next.next p = p.next return phead.next
def partition(self, head: ListNode, x: int) -> ListNode: s_pt = s_cur = ListNode() l_pt = l_cur = ListNode() while head: tmp = head.next if head.val < x: s_cur.next = head s_cur = s_cur.next else: l_cur.next = head l_cur = l_cur.next head.next = None head = tmp s_cur.next = l_pt.next return s_pt.next
def swapPairs(self, head): vhead = ListNode(0) vhead.next = head left = vhead middle = head right = None while middle is not None: right = middle.next if right is None: break middle.next = right.next left.next = right right.next = middle left = middle middle = left.next return vhead.next
def deleteDuplicates(self, head: ListNode) -> ListNode: if not head: return None sp = ListNode() sp.next = head pt = head pre = sp while pt and pt.next: if pt.val == pt.next.val: while pt.next and pt.val == pt.next.val: pt.next = pt.next.next pre.next = pt.next pt = pre.next else: pre = pt pt = pt.next return sp.next
def reverseKGroup(self, head: ListNode, k: int) -> ListNode: dummy = ListNode(0) dummy.next = head pre = dummy end = dummy while end.next is not None: for i in range(k): end = end.next if end is not None else None if end is None: break start = pre.next next = end.next end.next = None pre.next = self.reverse(start) start.next = next pre, end = start, start return dummy.next
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: rtn = ListNode() rtn.next = head mem = [rtn] slow = fast = head scnt = fcnt = 0 while fast and fast.next: mem.append(slow) slow = slow.next scnt += 1 fast = fast.next.next fcnt += 2 fcnt += 0 if not fast else 1 idx = fcnt - n + 1 while len(mem) < idx + 2 and slow: mem.append(slow) slow = slow.next if len(mem) < idx + 2: mem.append(None) mem[idx - 1].next = mem[idx + 1] return rtn.next
def reverseKGroup(self, head, k): if k < 2: return head vhead = ListNode(0) vhead.next = head #search for next k nodes def hasNodes(nodeHead, numNodes): c = nodeHead.next while numNodes > 0: if c is None: return False c = c.next numNodes = numNodes - 1 return True def reverseNodes(nodeHead, numNodes): c0 = nodeHead c1 = c0.next c2 = c1.next c3 = c1 n = numNodes - 1 while n > 0: t = c2.next c2.next = c1 c0.next = c2 c1 = c2 c2 = t n = n - 1 c3.next = t return c3 cur = vhead while hasNodes(cur, k) is True: cur = reverseNodes(cur, k) return vhead.next
from util import ListNode, printListNode #class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None # #def printListNode(head): # while (head): # print head.val # head = head.next node11 = ListNode(2) node12 = ListNode(4) node13 = ListNode(6) node11.next = node12 node12.next = node13 ''' 1: pre, 2(p)->4->6 1.1: 2(p)->4(tmp)->6 1.2: None(pre)<-2(p) 4->6 1.3: None<-2(prev/p) 4->6 1.4: None<-2(pre) 4(p)->6 2: None<-2(prev) 4(p)->6 2.1: None<-2(prev) 4(p)->6(tmp) 2.2: None<-2(prev)<-4(p) 6(tmp) 2.3: None<-2<-4(prev/p) 6 2.4: None<-2<-4(prev) 6(p) ... '''