list = [] cur = head while cur != None: list.append(cur.val) cur = cur.next n = len(list) tmp = [1] * n for i in range(n): tmp[(i + k) % n] = list[i] head = ListNode() cur = head if len(tmp) > 0: for e in tmp: node = ListNode(e) cur.next = node cur = node return head.next head = ListNode.gen([1, 2, 3, 4, 5]) k = 2 s = Solution() head = s.rotateRight(head, k) print ListNode.list(head)
class Solution(object): def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ if head == None or head.next == None: return head tmp = ListNode(-1, head) pre = tmp cur = head while cur != None: while cur.next != None and cur.val == cur.next.val: cur = cur.next pre.next = cur pre = cur cur = cur.next return tmp.next head = ListNode.gen([1, 1, 2, 3, 3]) s = Solution() head = s.deleteDuplicates(head) while head != None: print head head = head.next
:rtype: ListNode """ if head == None or head.next == None: return head tHead = ListNode() tmp = tHead cur = head while cur != None: if cur.next == None or cur.val != cur.next.val: tmp.next = cur tmp = cur else: while cur != None and cur.next != None and cur.val == cur.next.val: cur = cur.next cur = cur.next tmp.next = None return tHead.next head = ListNode.gen([1, 1, 1]) s = Solution() head = s.deleteDuplicates(head) c = 1 while head != None: print head.val head = head.next