和链表有关面试题
2015-12-11 18:09
453 查看
面试中被问链表的题目我就不再多说,直接总结题目。
1、将链表逆序
这个问题很早就研究过,但后来一次面试的时候我突然紧张忘了,没答上来。
我不知道大家的解法是什么,我的解法是遍历链表是用前插发插入节点,最后的链表就是逆序的。
[python] view
plaincopy
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def resverse(self, head):
print id(head)
if None == head or None == head.next:
return head
p = head.next
head.next = None
while None != p:
q = p.next
p.next = head
head = p
p = q
return head
2、判断链表中是否有环
这个问题似乎是有固定的解法即就是设置快、慢两个指针,若指针能相遇,则说明有环,如果慢指针为 None了,就说明没有环
[python] view
plaincopy
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def hasCycle(self, head):
if None == head or None == head.next:
return False
p = head
q = head.next
while q != None and p != q:
p = p.next
q = q.next
if None != q:
q = q.next
return p == q
3、既然链表中有环,求取环的长度
仔细想想,这其实和我们小学的追击问题是一样的,甲乙相遇的条件是 S甲 - S2 = n * 周长。另外,因为已经找到了环上的一个点,那么直接从该点开始,下次到该点时所走长度就是环的长度。
[python] view
plaincopy
class Solution:
def cycleLen(self, head):
if None == head or None == head.next:
return 0
p = head
q = head.next
while q != None and p != q:
p = p.next
q = q.next
if None != q:
q = q.next
if p != q:
return 0
ret_len = 1
p = p.next
while p != q:
ret_len += 1
p = p.next
return ret_len
4、判断两个链表的第一个公共点
求公共节点的时候需要分三种情况:两个都没环,一个有环,两个都没环,后两种先不讨论了。
[python] view
plaincopy
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def getLen(self, head):
len = 0
while head != None:
len += 1
head = head.next
return len
def getCommonNode(self, head1, head2):
len1 = self.getLen(head1)
len2 = self.getLen(head2)
p = head1
q = head2
while len1 - len2 > 0:
p = p.next
len1 -= 1
while len1 - len2 < 0:
q = q.next
len2 -= 1
while p != None:
if p == q:
return q
p = p.next
q = q.next
pass
5、单向链表中,如何在给定节点前快速插入一个节点?
大家都知道,链表插入节点时必须要得到插入位置的前一个节点,但这道题中并没有给出,那么我们是不是应该再遍历一次,找到插入位置的前一个节点?这样是可行的,但不是面试官想要的。那么应该怎样做呢?换个角度,我们在当前节点前插入节点是很难的,但是在当前节点后插入节点又是很容易的,所以我们可以先将节点插入到当前节点之后,然后交换一下数据。
[python] view
plaincopy
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def insertNode(self, index, node):
node.next = index.next
index.next = node
index.val, node.val = node.val, index.val
6、对于一个数组,将所有奇数移动到偶数前面
[python] view
plaincopy
def fun(self, arr):
p = 0
q = len(arr) - 1
while p < q:
while p < q and arr[p] & 0x01 != 0:
p += 1
while p < q and arr[q] & 0x01 == 0:
q -= 1
arr[p], arr[q] = arr[q], arr[p]
1、将链表逆序
这个问题很早就研究过,但后来一次面试的时候我突然紧张忘了,没答上来。
我不知道大家的解法是什么,我的解法是遍历链表是用前插发插入节点,最后的链表就是逆序的。
[python] view
plaincopy
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def resverse(self, head):
print id(head)
if None == head or None == head.next:
return head
p = head.next
head.next = None
while None != p:
q = p.next
p.next = head
head = p
p = q
return head
2、判断链表中是否有环
这个问题似乎是有固定的解法即就是设置快、慢两个指针,若指针能相遇,则说明有环,如果慢指针为 None了,就说明没有环
[python] view
plaincopy
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def hasCycle(self, head):
if None == head or None == head.next:
return False
p = head
q = head.next
while q != None and p != q:
p = p.next
q = q.next
if None != q:
q = q.next
return p == q
3、既然链表中有环,求取环的长度
仔细想想,这其实和我们小学的追击问题是一样的,甲乙相遇的条件是 S甲 - S2 = n * 周长。另外,因为已经找到了环上的一个点,那么直接从该点开始,下次到该点时所走长度就是环的长度。
[python] view
plaincopy
class Solution:
def cycleLen(self, head):
if None == head or None == head.next:
return 0
p = head
q = head.next
while q != None and p != q:
p = p.next
q = q.next
if None != q:
q = q.next
if p != q:
return 0
ret_len = 1
p = p.next
while p != q:
ret_len += 1
p = p.next
return ret_len
4、判断两个链表的第一个公共点
求公共节点的时候需要分三种情况:两个都没环,一个有环,两个都没环,后两种先不讨论了。
[python] view
plaincopy
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def getLen(self, head):
len = 0
while head != None:
len += 1
head = head.next
return len
def getCommonNode(self, head1, head2):
len1 = self.getLen(head1)
len2 = self.getLen(head2)
p = head1
q = head2
while len1 - len2 > 0:
p = p.next
len1 -= 1
while len1 - len2 < 0:
q = q.next
len2 -= 1
while p != None:
if p == q:
return q
p = p.next
q = q.next
pass
5、单向链表中,如何在给定节点前快速插入一个节点?
大家都知道,链表插入节点时必须要得到插入位置的前一个节点,但这道题中并没有给出,那么我们是不是应该再遍历一次,找到插入位置的前一个节点?这样是可行的,但不是面试官想要的。那么应该怎样做呢?换个角度,我们在当前节点前插入节点是很难的,但是在当前节点后插入节点又是很容易的,所以我们可以先将节点插入到当前节点之后,然后交换一下数据。
[python] view
plaincopy
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def insertNode(self, index, node):
node.next = index.next
index.next = node
index.val, node.val = node.val, index.val
6、对于一个数组,将所有奇数移动到偶数前面
[python] view
plaincopy
def fun(self, arr):
p = 0
q = len(arr) - 1
while p < q:
while p < q and arr[p] & 0x01 != 0:
p += 1
while p < q and arr[q] & 0x01 == 0:
q -= 1
arr[p], arr[q] = arr[q], arr[p]
相关文章推荐
- 目前程序员的5中情况
- 如何编写一个程序猿另一个面试官眼前一亮的简历
- 清除行列(程序员面试金典)
- 面试题3:二维数组中的查找(offer)
- Android重难点解析——面试中可能被问到的那些问题
- 面试中的智力题及编程实践
- 26岁程序员的出路
- 面试题2:实现单例模式(offer)
- 经验之谈!35岁程序员的独家面试经历
- 面试中需要注意的一些问题及准备
- 个人面试提问企业必问的问题
- 黑马程序员——反射
- 面试题1:赋值运算函数(offer)
- 黑马程序员——多线程
- 黑马程序员——Java重点基础之多线程(三) 注意
- 黑马程序员——Java基础---数据类型
- 黑马程序员——异常
- 十大编程算法助程序员走上高手之路
- 程序员是一群什么人?
- 前端开发面试总结系列一