Ejemplo n.º 1
0
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head:
            return None
        p = head
        pre = None
        while p.next:
            q = p.next
            if not pre:
                p.next, q.next = q.next, p
                head = q
            else:
                pre.next, p.next, q.next = q, q.next, p
            pre = p
            if p.next:
                p = p.next
            else:
                break
        return head


from Leetcode_medium.linkedlist.linkedlist import LinkedList
s = Solution()
x = [1, 2]

L = LinkedList(x)
L.display()
L.display(s.swapPairs(L.root))
Ejemplo n.º 2
0
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        root = pre = ListNode(0)
        root.next = head
        while head and head.next:
            if head.val == head.next.val:
                while head and head.next and head.val == head.next.val:
                    head = head.next
                head = head.next
                pre.next = head
            else:
                pre = pre.next
                head = head.next
        return root.next


from Leetcode_medium.linkedlist.linkedlist import LinkedList
s = Solution()
x = [1, 1, 2,2]

L = LinkedList(x)
L.display()
L.display(s.deleteDuplicates(L.root))
Ejemplo n.º 3
0
        if m == n:
            return head

        count = 1
        p = q = head
        res = []
        while count <= n:
            if count < m:
                p = p.next
            else:
                res.append(q.val)
            if count < n:
                q = q.next
            if count == n:
                print(res)
                for i in range(len(res)):
                    p.val = res[-i - 1]
                    p = p.next
            count += 1
        return head


from Leetcode_medium.linkedlist.linkedlist import LinkedList
s = Solution()
x = [1, 2, 3, 4, 5]
m = 2
n = 5
L = LinkedList(x)
L.display()
L.display(s.reverseBetween(L.root, m, n))
Ejemplo n.º 4
0
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None


class Solution(object):
    def rotateRight(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """


from Leetcode_medium.linkedlist.linkedlist import LinkedList
s = Solution()
x = [1, None]
n = 2
L = LinkedList(x)
L.display()
L.display(s.rotateRight(L.root, n))
Ejemplo n.º 5
0
class Solution(object):
    def partition(self, head, x):
        """
        :type head: ListNode
        :type x: int
        :rtype: ListNode
        """
        d1 = large = ListNode(-1)
        d2 = small = ListNode(0)
        cur = head

        while cur:
            if cur.val < x:
                small.next = cur
                small = small.next
            else:
                large.next = cur
                large = large.next
            cur = cur.next
        large.next = None
        small.next = d1.next
        return d2.next


from Leetcode_medium.linkedlist.linkedlist import LinkedList
s = Solution()
x = [1, 4, 3, 2, 5, 2]
n = 3
L = LinkedList(x)
L.display()
L.display(s.partition(L.root, n))
Ejemplo n.º 6
0
class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        if n == 0:
            return head
        p = q = head
        while p.next:
            if n > 0:
                n -= 1
            else:
                q = q.next
            p = p.next
        if n:
            head = head.next
        else:
            q.next = q.next.next
        return head

from Leetcode_medium.linkedlist.linkedlist import LinkedList
s = Solution()
x = [1,2,3,4,5]
n = 5
L = LinkedList(x)
L.display()
L.display(s.removeNthFromEnd(L.root, n))
Ejemplo n.º 7
0
# class RandomListNode(object):
#     def __init__(self, x):
#         self.label = x
#         self.next = None
#         self.random = None
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def copyRandomList(self, head):
        """
        :type head: RandomListNode
        :rtype: RandomListNode
        """
        if head:
            p = ListNode(head.val)
            if head.next:
                p.next = self.copyRandomList(head.next)
            return p
        return head


from Leetcode_medium.linkedlist.linkedlist import LinkedList
s = Solution()
x = [1,2,3,4,5]

L = LinkedList(x)
L.display()
L.display(s.copyRandomList(L.root))