示例#1
0
# Implement an algorithm to delete a node in the middle of a singly linked list,
#  given only access to that node.

# Have you met this question in a real interview? Yes
# Example
# Given 1->2->3->4, and node 3. return 1->2->4

from ListNode import ListNode


def deleteNodeintheMiddleofSinglyLinkedList(node):
    if node is None:
        return
    # node is not last node
    if node.next is not None:
        node.val = node.next.val
        node.next = node.next.next
    # node is the last node
    else:
        node = None


head = ListNode.arrayToList([1, 2, 3, 4])
node = head.next.next
deleteNodeintheMiddleofSinglyLinkedList(node)
ListNode.printList(head)
示例#2
0
# Given a  singly linked list L: L0 -> L1 -> ... ->Ln-1 -> Ln,
# reorder it to: L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 -> ...

# You must do this in-place without altering the nodes' values.

# For example,
# Given 1 -> 2 -> 3 -> 4 -> null, reorder it to 1 -> 4 -> 2 -> 3 -> null.

from ListNode import ListNode

def reorderList(head):
    if head == None:
        return None


head = ListNode.arrayToList([1, 4, 2, 3])
ListNode.printList(reorderList(head))
# For example,
# Given 1->4->3->2->5->2->null and x = 3,
# return 1->2->2->4->3->5->null.

from ListNode import ListNode

def partitionList(head, x):

    # create two list:left and right
    leftDummy = ListNode(0)
    leftTail = leftDummy
    rightDummy = ListNode(0)
    rightTail = rightDummy

    while head != None:
        if head.val < x:
            leftTail.next = head
            leftTail = leftTail.next
        else:
            rightTail.next = head
            rightTail = rightTail.next
        head = head.next

    # combine two list
    rightTail.next = None
    leftTail.next = rightDummy.next

    return leftDummy.next

ListNode.printList(partitionList(ListNode.arrayToList([1, 4, 3, 2, 5, 2]), 3))
示例#4
0
        slow = slow.next
        fast = fast.next.next
    return slow


# helper: merge two sorted linked list
def merge(head1, head2):
    if head1 == None and head2 == None:
        return None

    dummy = ListNode(0)
    lastNode = dummy
    while head1 != None and head2 != None:
        if head1.val < head2.val:
            lastNode.next = head1
            head1 = head1.next
        else:
            lastNode.next = head2
            head2 = head2.next
        lastNode = lastNode.next
    if head1 != None:
        lastNode.next = head1
    else:
        lastNode.next = head2
    return dummy.next


head = ListNode.arrayToList([1, 3, 2, 4, 5, 7, 6])
ListNode.printList(sortList(head))
ListNode.printList(sortListI(head))
# Reverse a linked list.

# Have you met this question in a real interview? Yes
# Example
# For linked list 1->2->3, the reversed linked list is 3->2->1

# Challenge
# Reverse it in-place and in one-pass

from ListNode import ListNode


def reverseLinkedList(head):
    prev = None
    while head != None:
        temp = head.next
        head.next = prev
        prev = head
        head = temp
    return prev


head = ListNode.arrayToList([1, 2, 3])
ListNode.printList(reverseLinkedList(head))
    tail = dummy
    carry = 0
    while l1 is not None and l2 is not None:
        s = l1.val + l2.val + carry
        tail.next = ListNode(s % 10)
        carry = s / 10
        l1 = l1.next
        l2 = l2.next
        tail = tail.next
    while l1 is not None:
        s = l1.val + carry
        tail.next = ListNode(s % 10)
        carry = s / 10
        l1 = l1.next
        tail = tail.next
    while l2 is not None:
        s = l2.next + carry
        tail.next = ListNode(s % 10)
        carry = s / 10
        l2 = l2.next
        tail = tail.next
    if carry != 0:
        tail.next = ListNode(carry)

    return dummy.next


l1 = ListNode.arrayToList([6, 1, 7])
l2 = ListNode.arrayToList([2, 9, 5])
ListNode.printList(addTwoNumbersII(l1, l2))
示例#7
0
# Remove all elements from a linked list of integers that have value val.

# Have you met this question in a real interview? Yes
# Example
# Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5

from ListNode import ListNode


def removeLinkedListElements(head, val):
    if head == None or val == None:
        return head
    dummy = ListNode(0)
    dummy.next = head
    head = dummy
    while head.next != None:
        if head.next.val == val:
            head.next = head.next.next
        else:
            head = head.next
    return dummy.next


head = ListNode.arrayToList([1, 2, 3, 3, 4, 5, 3])
ListNode.printList(removeLinkedListElements(head, 3))
# Given 1->3->8->11->15->null, 2->null , return 1->2->3->8->11->15->null.

from ListNode import ListNode


def mergeTwoSortedLists(l1, l2):
    if l1 == None and l2 == None:
        return None
    dummy = ListNode(0)
    tail = dummy
    while l1 != None and l2 != None:
        if l1.val < l2.val:
            tail.next = l1
            l1 = l1.next
        else:
            tail.next = l2
            l2 = l2.next
        tail = tail.next
    if l1 != None:
        tail.next = l1
    else:
        tail.next = l2

    return dummy.next


head1 = ListNode.arrayToList([1, 3, 8, 11, 15])
head2 = ListNode.arrayToList([2])

ListNode.printList(mergeTwoSortedLists(head1, head2))
    mid = start + (end - start) / 2
    left = mergeKHelper(lists, start, mid)
    right = mergeKHelper(lists, mid + 1, end)
    return mergeTwoSortedList(left, right)

def mergeTwoSortedList(head1, head2):
    if head1 == None and head2 == None:
        return None
    dummy = ListNode(0)
    tail = dummy
    while head1 != None and head2 != None:
        if head1.val < head2.val:
            tail.next = head1
            head1 = head1.next
        else:
            tail.next = head2
            head2 = head2.next
        tail = tail.next
    if head1 != None:
        tail.next = head1
    else:
        tail.next = head2
    return dummy.next




head1 = ListNode.arrayToList([2, 4])
head2 = ListNode.arrayToList([-1])
ListNode.printList(mergeKSortedLists([head1, head2]))
def insertionSortList(head):
    if head == None or head.next == None:
        return head
    dummy = ListNode(0)
    dummy.next = head
    prev = head
    head = head.next
    while head != None:
        temp1 = head.next
        iNode = dummy
        while iNode.next != head:

            if iNode.next.val > head.val:
                temp2 = iNode.next
                iNode.next = head
                head.next = temp2
                prev.next = temp1
            else:
                iNode = iNode.next
        prev = head
        head = temp2

    return head


print 1

head = ListNode.arrayToList([1, 3, 2, 0])
ListNode.printList(insertionSortList(head))
示例#11
0
# After removing the second node from the end, the linked list becomes
# 1->2->3->5->null.
# Note
# The minimum number of nodes in list is n.

# Challenge
# O(n) time

from ListNode import ListNode

def removeNthNodefromEndofList(head, n):
    if head == None and not n:
        return head
    dummy = ListNode(0)
    dummy.next = head
    slow = dummy
    fast = dummy
    for i in range(n):
        fast = fast.next
    while fast.next != None:
        slow = slow.next
        fast = fast.next

    slow.next = slow.next.next
    return dummy.next


head = ListNode.arrayToList([1, 2, 3, 4, 5])
ListNode.printList(removeNthNodefromEndofList(head, 2))
示例#12
0
# return 1->2->2->4->3->5->null.

from ListNode import ListNode


def partitionList(head, x):

    # create two list:left and right
    leftDummy = ListNode(0)
    leftTail = leftDummy
    rightDummy = ListNode(0)
    rightTail = rightDummy

    while head != None:
        if head.val < x:
            leftTail.next = head
            leftTail = leftTail.next
        else:
            rightTail.next = head
            rightTail = rightTail.next
        head = head.next

    # combine two list
    rightTail.next = None
    leftTail.next = rightDummy.next

    return leftDummy.next


ListNode.printList(partitionList(ListNode.arrayToList([1, 4, 3, 2, 5, 2]), 3))
    head = dummy

    # find premmNode and mNode
    for i in range(1, m):
        head = head.next
    prevmNode = head
    mNode = head.next

    # reverse link from m to n
    nNode = mNode
    nextnNode = nNode.next
    for i in range(m, n):
        temp = nextnNode.next
        nextnNode.next = nNode
        nNode = nextnNode
        nextnNode = temp

    # combine
    prevmNode.next = nNode
    mNode.next = nextnNode

    return dummy.next


head = ListNode.arrayToList([1, 2, 3, 4, 5])
ListNode.printList(reverseLinkedListII(head, 2, 4))




示例#14
0
# Given a sorted linked list, delete all duplicates such that each element
# appear only once.

# Have you met this question in a real interview? Yes
# Example
# Given 1->1->2, return 1->2.
# Given 1->1->2->3->3, return 1->2->3.

from ListNode import ListNode

def removeDuplicatesfromSortedList(head):
    if head == None:
        return None
    node = head
    while node.next != None:
        if node.val == node.next.val:
            node.next = node.next.next
        else:
            node = node.next

    return head


# head1 = ListNode.arrayToList([1, 1, 2])
# ListNode.printList(removeDuplicatesfromSortedList(head1))
head2 = ListNode.arrayToList([1, 1, 2, 3, 3])
ListNode.printList(removeDuplicatesfromSortedList(head2))

示例#15
0
    if head == None:
        return None
    dummy = ListNode(0)
    dummy.next = head
    head = dummy

    # find premmNode and mNode
    for i in range(1, m):
        head = head.next
    prevmNode = head
    mNode = head.next

    # reverse link from m to n
    nNode = mNode
    nextnNode = nNode.next
    for i in range(m, n):
        temp = nextnNode.next
        nextnNode.next = nNode
        nNode = nextnNode
        nextnNode = temp

    # combine
    prevmNode.next = nNode
    mNode.next = nextnNode

    return dummy.next


head = ListNode.arrayToList([1, 2, 3, 4, 5])
ListNode.printList(reverseLinkedListII(head, 2, 4))
# Remove all elements from a linked list of integers that have value val.

# Have you met this question in a real interview? Yes
# Example
# Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5

from ListNode import ListNode

def removeLinkedListElements(head, val):
    if head == None or val == None:
        return head
    dummy = ListNode(0)
    dummy.next = head
    head = dummy
    while head.next != None:
        if head.next.val == val:
            head.next = head.next.next
        else:
            head = head.next
    return dummy.next

head = ListNode.arrayToList([1, 2, 3, 3, 4, 5, 3])
ListNode.printList(removeLinkedListElements(head, 3))

# Implement an algorithm to delete a node in the middle of a singly linked list,
#  given only access to that node.

# Have you met this question in a real interview? Yes
# Example
# Given 1->2->3->4, and node 3. return 1->2->4

from ListNode import ListNode

def deleteNodeintheMiddleofSinglyLinkedList(node):
    if node is None:
        return
    # node is not last node
    if node.next is not None:
        node.val = node.next.val
        node.next = node.next.next
    # node is the last node
    else:
        node = None


head = ListNode.arrayToList([1, 2, 3, 4])
node = head.next.next
deleteNodeintheMiddleofSinglyLinkedList(node)
ListNode.printList(head)
示例#18
0
# Example
# Given 1->2->3->4, you should return the list as 2->1->4->3.

# Challenge
# Your algorithm should use only constant space. You may not modify the values
# in the list, only nodes itself can be changed.

from ListNode import ListNode

def swapNodesinPairs(head):
    if head == None:
        return None
    dummy = ListNode(0)
    dummy.next = head
    head = dummy
    while head != None and head.next != None:
        n1 = head.next
        n2 = head.next.next
        # head -> n1 -> n2 ... => head -> n2 -> n1 ...
        head.next = n2
        n1.next = n2.next
        n2.next = n1

        # move to next pair
        head = n1
    return dummy.next


head = ListNode.arrayToList([1, 2, 3, 4])
ListNode.printList(swapNodesinPairs(head))
from ListNode import ListNode

def mergeTwoSortedLists(l1, l2):
    if l1 == None and l2 == None:
        return None
    dummy = ListNode(0)
    tail = dummy
    while l1 != None and l2 != None:
        if l1.val < l2.val:
            tail.next = l1
            l1 = l1.next
        else:
            tail.next = l2
            l2 = l2.next
        tail = tail.next
    if l1 != None:
        tail.next = l1
    else:
        tail.next = l2

    return dummy.next




head1 = ListNode.arrayToList([1, 3, 8, 11, 15])
head2 = ListNode.arrayToList([2])

ListNode.printList(mergeTwoSortedLists(head1, head2))

# Example
# Given 1->2->3->3->4->4->5, return 1->2->5.
# Given 1->1->1->2->3, return 2->3.

from ListNode import ListNode


def removeDupfromSortedListII(head):
    if head == None:
        return None

    dummy = ListNode(0)
    dummy.next = head
    head = dummy

    while head.next != None and head.next.next != None:
        if head.next.val == head.next.next.val:
            val = head.next.val
            while head.next != None and head.next.val == val:
                head.next = head.next.next
        else:
            head = head.next

    return dummy.next


head1 = ListNode.arrayToList([1, 2, 3, 3, 4, 4, 5])
head2 = ListNode.arrayToList([1, 1, 1, 2, 3])

ListNode.printList(removeDupfromSortedListII(head2))
# Have you met this question in a real interview? Yes
# Example
# Given 1->3->2->1->4.

# Return 1->3->2->4

# Challenge
# (hard) How would you solve this problem if a temporary buffer is not allowed?
# In this case, you don't need to keep the order of nodes.

from ListNode import ListNode

def removeDuplicatesfromUnsortedList(head):
    if head == None:
        return None
    dummy = ListNode(0)
    dummy.next = head
    head = dummy
    s = set()
    while head.next != None:
        if head.next.val in s:
            head.next = head.next.next
        else:
            s.add(head.next.val)
            head = head.next
    return dummy.next


head = ListNode.arrayToList([1, 3, 2, 1, 4])
ListNode.printList(removeDuplicatesfromUnsortedList(head))
示例#22
0
        slow = slow.next
        fast = fast.next.next
    return slow


# helper: merge two sorted linked list
def merge(head1, head2):
    if head1 == None and head2 == None:
        return None

    dummy = ListNode(0)
    lastNode = dummy
    while head1 != None and head2 != None:
        if head1.val < head2.val:
            lastNode.next = head1
            head1 = head1.next
        else:
            lastNode.next = head2
            head2 = head2.next
        lastNode = lastNode.next
    if head1 != None:
        lastNode.next = head1
    else:
        lastNode.next = head2
    return dummy.next


head = ListNode.arrayToList([1, 3, 2, 4, 5, 7, 6])
ListNode.printList(sortList(head))
ListNode.printList(sortListI(head))
    tail = dummy
    carry = 0
    while l1 is not None and l2 is not None:
        s = l1.val + l2.val + carry
        tail.next = ListNode(s % 10)
        carry = s / 10
        l1 = l1.next
        l2 = l2.next
        tail = tail.next
    while l1 is not None:
        s = l1.val + carry
        tail.next = ListNode(s % 10)
        carry = s / 10
        l1 = l1.next
        tail = tail.next
    while l2 is not None:
        s = l2.next + carry
        tail.next = ListNode(s % 10)
        carry = s / 10
        l2 = l2.next
        tail = tail.next
    if carry != 0:
        tail.next = ListNode(carry)

    return dummy.next


l1 = ListNode.arrayToList([6, 1, 7])
l2 = ListNode.arrayToList([2, 9, 5])
ListNode.printList(addTwoNumbersII(l1, l2))
示例#24
0
def removeDupfromSortedListII(head):
    if head == None:
        return None

    dummy = ListNode(0)
    dummy.next = head
    head = dummy

    while head.next != None and head.next.next != None:
        if head.next.val == head.next.next.val:
            val = head.next.val
            while head.next != None and head.next.val == val:
                head.next = head.next.next
        else:
            head = head.next

    return dummy.next


head1 = ListNode.arrayToList([1, 2, 3, 3, 4, 4, 5])
head2 = ListNode.arrayToList([1, 1, 1, 2, 3])

ListNode.printList(removeDupfromSortedListII(head2))