链表相关算法整理【实时更新】

1. 链表删除重复节点

  • 重复节点不保留:双重循环
class Solution:
    def deleteDuplication(self, p):
        # write code here
        root = ListNode(0)
        root.next = p
        pre = root
        while p:
            if p.next and p.val == p.next.val:
                while p.next and p.val == p.next.val:
                    p.next = p.next.next
                p = p.next
                pre.next = p 
            else:
                pre = p
                p = p.next
  • 重复节点保留:单循环
# 待补充

2. 链表倒数第k个节点

  • 快慢指针
class Solution:
    def FindKthToTail(self, head, k):
        # write code here
        slow = head
        fast = head
        for _ in range(k):
            if not fast:
                return None
            fast = fast.next
        while fast:
            slow = slow.next
            fast = fast.next
        return slow

3. 反转链表

  • 三指针
class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        # write code here
        head = pHead
        if head == None or head.next == None:
            return head
        pre = None
        while head:
            nex = head.next # 记录当前节点的下一节点
            head.next = pre # 当前节点指向取反
            pre = head      # nex向前走一步
            head = nex      # head向前走一步
        return pre

4. 合并排序链表

class Solution:
    # 返回合并后列表
    def Merge(self, p1, p2):
        # write code here
        cur = head = ListNode(0) # 建立helper指针
        while p1 and p2:
            if p1.val<=p2.val:
                cur.next = p1
                p1 = p1.next
            else:
                cur.next = p2
                p2 = p2.next
            cur = cur.next
        if p1 == None:
            cur.next = p2
        elif p2 == None:
            cur.next = p1
        else:
            cur.next = None
        return head.next

5. 两链表的第一个公共节点

class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
        if (pHead1 == NULL || pHead2 == NULL){
            return NULL;
        }
        ListNode* p1 = pHead1;
        ListNode* p2 = pHead2;
        while(p1 != p2){
            p1 = p1->next;
            p2 = p2->next;
            if(p1 != p2){
                if(p1 == NULL) p1 = pHead2;
                if(p2 == NULL) p2 = pHead1;
            }
        }
        return p1; 
    }
};

6. 链表中环的入口

  • 快慢指针相遇确定有环
  • slow2从head开始移动,slow从相遇点开始移动,交点就是环的入口
class Solution:
    def EntryNodeOfLoop(self, phead):
        # write code here
        if not phead:
            return None
        slow = phead
        fast = phead
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                slow2 = phead
                while slow !=slow2:
                    slow = slow.next
                    slow2 = slow2.next
                return slow
        return None
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值