1.Easy:Reverse Linked List
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
思路
:
- 头插法和递归法,参考 这个博客
- 头插法和递归法,参考 这个博客
2.Easy: Odd Even Linked List
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 1:
Input: 1->2->3->4->5->NULL
Output: 1->3->5->2->4->NULL
Example 2:
Input: 2->1->3->5->6->4->7->NULL
Output: 2->3->6->7->1->5->4->NULL
Note:
The relative order inside both the even and odd groups should remain as it was in the input.
The first node is considered odd, the second node even and so on …
思路:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None or head.next == None:
return head
odd = head
even = head.next
node = even
while even and even.next:
odd.next = even.next
odd = odd.next
even.next = odd.next
even = even.next
odd.next = node
return head
–Easy:Add Two Numbers
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.
思路:注意累加超过10的情况
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if not l1:
return l2
if not l2:
return l1
res = ListNode((l1.val+l2.val)%10)
start = res
pre = (l1.val+l2.val)//10
while l1.next and l2.next:
l1 = l1.next
l2 = l2.next
res.next = ListNode((l1.val+l2.val+pre)%10)
pre = (l1.val+l2.val+pre)//10
res = res.next
#处理两个链表不同长问题
rest = None
if l1.next:
rest = l1.next
elif l2.next:
rest = l2.next
while rest:
res.next = ListNode((rest.val + pre)%10)
pre = (rest.val + pre)//10
res = res.next
rest = rest.next
#处理[5],[5]这种情况
while pre:
res.next = ListNode(pre%10)
pre = pre//10
res = res.next
return start
3.–Mid:Reverse Linked List II
Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
思路:使用头插法逆置
参考博客配图
class Solution:
def reverseBetween(self, head, m, n):
"""
:type head: ListNode
:type m: int
:type n: int
:rtype: ListNode
"""
if not head or m == n:
return head
#虚拟节点,使得m=1这种情况有前面可以头插连接的位置
dummy = ListNode(0)
dummy.next = head
cur = dummy
for i in range(m-1):
cur = cur.next
pre = cur.next
pos = pre.next
for i in range(m,n):
pre.next = pos.next
pos.next = cur.next
cur.next = pos
pos = pre.next
return dummy.next