链表
aaaqqq1234
这个作者很懒,什么都没留下…
展开
-
回文链表
题目判断一个链表是否是回文链表思路找链表中点反转后一段链表然后依次判断两个链表值是否相等def huiwenlink(link): head = link.head fast = slow = head.next while fast.next and fast.next.next: fast = fast.next.next slow = slow.next mid = slow rpre = Node() rhead_cur = mid.next原创 2020-07-18 21:22:20 · 73 阅读 · 0 评论 -
删除重复链表
题目删除重复链表1–1--1–2--3return 2–3思路设置一个哨兵,一个pre=哨兵,然后比较cur和curnext,若相等curnext向前移一个,若不相等,则cur移动到curnext,然后在判断当前cur和curnext,若不相等,pre连上cur,否则不连。代码def remove_repeat2(link): head = link.head cur = head.next cur_next = cur.next dummy = Node() pre =原创 2020-07-18 19:32:18 · 57 阅读 · 0 评论 -
删除链表的重复元素
题目删除链表的重复元素1–2--2–3—3–4return 1–2--3–4思路两个指针,一个cur,一个cur_next,比较cur和cur_next,若相等则cur_next后移,若不等则cur.next = cur_next.代码def remove_repeat(link): head = link.head cur = head.next cur_next = cur.next while cur_next is not None: if cur.value原创 2020-07-18 18:36:53 · 86 阅读 · 0 评论 -
k组链表反转
题目12345678k=2, 21436587k=3, 32165478思路第一个for循环先找到第一个k的end index,然后反转这一段字符串,反转的过程中,再找到下一段的截至位置,重复下去。代码def reverse_kpair(link,k): head = link.head head_next = head for i in range(k): if head_next is None: return head head_next = he原创 2020-07-18 18:01:18 · 195 阅读 · 1 评论 -
指定位置的反转链表
题目反转第m位到第n位的链表思路第一个for循环记录翻转前的位置,第二个for循环记录开始反转的位置,最后在总体链接。代码def inplace_reverse_mn(link,start,end): head = link.head cur = head precur = cur pre = Node() for i in range(start-1): precur = precur.next for i in range(start): cur = c原创 2020-07-18 15:59:03 · 320 阅读 · 0 评论 -
链表的反转
题目1234554321inplace思路创建三个指针,,往回指。代码def inplace_reverse(link): pre = Node() head = pre cur = link.head.next while cur is not None: n = cur.next cur.next = pre pre = cur result = cur cur = n return result...原创 2020-07-18 15:03:13 · 72 阅读 · 0 评论 -
分区链表
题目给定一个链表和数值x,使得所有小于x的链表在大于或等于x的链表之前思路从投到尾遍历链表,创建两个新链表,小的放一个,大的放一个,然后链接起来。代码def partition(link,num): linkhead = link.head rlink = Linkedlist() llink = Linkedlist() rlink_hand = rlink.head rlink_node = rlink_hand llink_head = llink.head ll原创 2020-07-18 14:10:15 · 364 阅读 · 0 评论 -
归并排序两个链表
标题归并排序两个链表思路递归+中点分割链表+两个链表合并def findmid(head): fast = head slow = head while fast.next is not None and fast.next.next is not None: fast = fast.next.next slow = slow.next rhead = slow.next slow.next = None return head,rhead def me原创 2020-07-16 22:39:06 · 177 阅读 · 0 评论 -
对链表插入排序
题目对链表插入排序思路初始换一个哨兵和cur,pre每次开始都指向dummy,然后比较pre-next和cur的值,插入,然后cur指向下一个。def insert_sortlink(link): dummy = Node() cur = link.head.next while cur is not None: pre = dummy while pre.next is not None and pre.next.value < cur.value:原创 2020-07-16 18:03:19 · 89 阅读 · 0 评论 -
找出两个链表的公共部分的起始位置
题目a1-a2-c1-c2-c3b1-b2-b3-c1-c2-c3思路第一种方法,找出两个链表的长度差,然后让长的那个先跑,然后两个在一起跑。第二种方法,a跑完在以b去跑,同理B跑完以A去跑,当A=B时,则说明找到了公共点代码def getcommonnode(l1,l2): curA = l1.head curB = l2.head lenA = 0 lenB = 0 while curA is not None: curA = curA.next len原创 2020-07-16 16:54:13 · 259 阅读 · 0 评论 -
合并两个有序链表
题目合并两个有序链表1–>2–>4,1–>3–>41–>1–>2–>3–>4–>4思路一次比较两个链表的每个元素,比较小的那个连到cur上,当前指针指向下一个。直到结束。代码def mergetowsortlink(link1,link2): dummy = cur = Node() l1 = link1.head.next l2 = link2.head.next while l1 is not None and l原创 2020-07-16 16:11:28 · 88 阅读 · 0 评论 -
分割链表
题目把一个链表平分两个部分思路双指针找中点代码def cut(lst): head = lst.head fast = head slow = head cut_node = slow if head is None: return 0 while fast is not None and fast.next is not None: fast = fast.next.next slow = slow.next cut_node = slow原创 2020-07-15 22:21:27 · 64 阅读 · 0 评论 -
判断链表是否有环
题目判断链表是否有环思路双指针,如果fast == slow 则就有环代码def circle(lst): head = lst.head fast = head slow = head if head is None: return False while fast is not None and fast.next is not None: fast = fast.next.next slow = slow.next if fast == s原创 2020-07-15 20:06:07 · 52 阅读 · 0 评论 -
查找链表中间数
题目查找链表的中间节点思路双指针,一个指针跑两步,另一个指针跑一步,就能找到中间节点。代码def find_mid(lst): assert lst.head is not None and lst.head.next is not None head = lst.head fast = head slow = head while fast is not None and fast.next is not None: fast = fast.next.next原创 2020-07-15 19:50:39 · 201 阅读 · 0 评论 -
链表的基本操作
class Node: def __init__(self,value=None,next=None): self.value = value self.next = next class Linkedlist: def __init__(self): self.head = Node() self.size = 0 def add_first(self,value): node = Node(value,None) node.next =原创 2020-07-15 18:00:25 · 105 阅读 · 0 评论