输出链表的值
# -*- coding:utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def printnode(self,node):
while node:
print(node.val)
node=node.next
if __name__=='__main__':
#1--》2--》3--》None
n1=ListNode(1)
n2 = ListNode(2)
n3 = ListNode(3)
n1.next=n2
n2.next=n3
n3.next=None
s=Solution()
s.printnode(n1)
# 结果
# 1
# 2
# 3
一输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。(nowcoder)
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回从尾部到头部的列表值序列,例如[1,2,3]
def printListFromTailToHead(self, listNode):
plist=[]
while listNode:
plist.insert(0,listNode.val)
listNode=listNode.next
return plist
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
# write code here
previous=None
cur=pHead
l=[]
while cur:
nextNode=cur.next
cur.next=previous
previous=cur
cur=nextNode
l.insert(0,pre.val)
return l
#return previous
二将两个有序链表合并为一个新的有序链表并返回。(nowcoder)
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
#非递归版本
class Solution:
# 返回合并后列表
def Merge(self, pHead1, pHead2):
# write code here
#初始化
tmp = ListNode(0)
pHead = tmp
while pHead1 and pHead2:
if pHead1.val < pHead2.val:
tmp.next = pHead1
pHead1 = pHead1.next
else:
tmp.next = pHead2
pHead2 = pHead2.next
tmp = tmp.next
if not pHead1:
tmp.next = pHead2
if not pHead2:
tmp.next = pHead1
return pHead.next
#递归版本
class Solution:
# 返回合并后列表
def Merge(self, pHead1, pHead2):
# write code here
if pHead1 is None:
return pHead2
if pHead2 is None:
return pHead1
if pHead1.val < pHead2.val:
pHead1.next = self.Merge(pHead1.next,pHead2)
return pHead1
else:
pHead2.next = self.Merge(pHead1,pHead2.next)
return pHead2
三将K个有序链表合并为一个新的有序链表并返回。(leetcode)
思路:
- 遍历所有链表,将所有节点的值放到一个数组中。
- 将这个数组排序,然后遍历所有元素得到正确顺序的值。
- 用遍历得到的值,创建一个新的有序链表。
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def mergeKLists(self, lists): """ :type lists: List[ListNode] :rtype: ListNode """ self.nodes = [] head = point = ListNode(0) for l in lists: while l: self.nodes.append(l.val) l = l.next for x in sorted(self.nodes): point.next = ListNode(x) point = point.next return head.next
四 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。(leetcode)
- 思路:
由于只输入了需要删除的节点node,因此无法获取删除节点node的前一个节点pre,从而也就无法将前一个节点pre指向删除节点的下一个节点nex;
既然无法通过修改指针完成,那么肯定要修改链表节点的值了。
将删除节点node的值和指针都改为下一个节点nex的值和指针即可。# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def deleteNode(self, node): """ :type node: ListNode :rtype: void Do not return anything, modify node in-place instead. """ nextnode=node.next after_nextnode=node.next.next node.val=nextnode.val node.next=after_nextnode
-
五 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5(nowcoder)
-
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def deleteDuplication(self, pHead): res = [] tmp = [] # 先转换成列表 while pHead: res.append(pHead.val) pHead = pHead.next # 在删除重复的节点 for i in range(len(res)): if res.count(res[i]) == 1: tmp.append(res[i]) # 然后再转成链表 dummy = ListNode(0) pre = dummy for i in tmp: node = ListNode(i) pre.next = node pre = pre.next return dummy.next
六输入一个链表,输出该链表中倒数第k个结点。
-
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def FindKthToTail(self, head, k): # write code here l=[] while head!=None: l.append(head) head=head.next if k>len(l) or k<1: return else: return l[-k]
七输入两个链表,找出它们的第一个公共结点。
-
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def FindFirstCommonNode(self, head1, head2): # write code here list1 = [] list2 = [] node1 = head1 node2 = head2 while node1: list1.append(node1.val) node1 = node1.next while node2: if node2.val in list1: return node2 else: node2 = node2.next
八给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
-
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def EntryNodeOfLoop(self, pHead): # write code here #遍历链表,环的存在,遍历遇见的第一个重复的即为入口节点 tempList = [] p = pHead while p: if p in tempList: return p else: tempList.append(p) p = p.next