两数相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
Code(By myself):
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
p = l1
q = l2
len1 = 0
len2 = 0
while p != None:
len1 += 1
p = p.next
while q != None:
len2 += 1
q = q.next
result = l1 if len1 > len2 else l2
m = result
flag = 0
p = l1
q = l2
while p != None and q != None:
m.val = p.val + q.val + flag
if m.val >= 10:
m.val -= 10
flag = 1
else:
flag = 0
m = m.next
p = p.next
q = q.next
if len1 > len2:
while p != None:
m.val = p.val + flag
if m.val >= 10:
m.val -= 10
flag = 1
else:
flag = 0
p = p.next
m = m.next
else:
while q != None:
m.val = q.val + flag
if m.val >= 10:
m.val -= 10
flag = 1
else:
flag = 0
q = q.next
m = m.next
if flag:
p = result
while p.next != None:
p = p.next
n = ListNode(1)
n.next = None
p.next = n
return result
Code(others):
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if not l1 or not l2:
return l1 or l2
root, add = l1, 0
while l1 and l2:
cur = l1
add, cur.val = divmod(l1.val + l2.val + add, 10)
l1, l2 = l1.next, l2.next
cur.next = l1 or l2
while add:
if cur.next:
cur = cur.next
add, cur.val = divmod(cur.val + add, 10)
else:
cur.next = ListNode(1)
break
return root
总结:
divmod()返回一个包含商和余数的元组(a // b, a % b)
奇偶链表
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.
You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.
Example:
Input:1->2->3->4->5->NULL
Output:1->3->5->2->4->NULL
Code(By myself):
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None or head.next == None or head.next.next == None:
return head
odd = head
temp = head.next
even = head.next
p = even.next
n = 3
while p != None:
if n % 2 != 0:
odd.next = p
odd = p
else:
even.next = p
even = p
p = p.next
n += 1
odd.next = temp
even.next = None
return head
Code(others):
class Solution(object):
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head is None:
return head
odd = head
even = odd.next
evenHead = even
while even is not None and even.next is not None:
odd.next = even.next
even.next = even.next.next
even = even.next
odd = odd.next
odd.next = evenHead
return head
总结:
奇偶交叉前进即可,无需计数
相交链表
write a program to find the node at which the intersection of two singly linked lists begins.
Example:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
Code(By myself):
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
if headA == None or headB == None:
return None
len1 = 0
len2 = 0
p = headA
q = headB
while p != None:
len1 += 1
p = p.next
while q != None:
len2 += 1
q = q.next
diff = abs(len1 - len2)
p = headA
q = headB
while diff:
if len1 > len2:
p = p.next
diff -= 1
else:
q = q.next
diff -= 1
while p != None and q != None:
if p == q:
return p
p = p.next
q = q.next
return None
Code(others):
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
#双指针法,制定两个指针pA pB,分别用这两个指针遍历A+B,如果两个指针在某点相遇,则该点就是链表的交点
#A=[1,3,5,7,9,11]
#B=[2,4,6,8,9,11]
#A+B的链表长度是一定的,如果有交点,那么这个长链表从后向前看的前几个必定是相同的
if not headA or not headB:
return None
pA=headA
pB=headB
while pA and pB and pA!=pB:
pA=pA.next
pB=pB.next
if pA==pB:
return pA
if not pA:
pA=headB
if not pB:
pB=headA
return pA
总结:
两链表相交之后的节点完全相同,长度不同时,将短链表遍历结束后,短链表指针指向长链表继续遍历,长链表指针遍历结束之后指向锻炼表,这时两链表指针后的节点数正好相等。