数据结构-链表

一、定义
1.定义

链表中每个节点有两部分组成,一是节点的value值,二是下一个节点在内存的地址。
要注意的几个地方:

  • 1.链表默认指向的是头节点,我们用while pHead:循环的时候需要用 pHead = pHead.next,否则一直在头节点循环
二、题目
题目1、链表反转
1.整体思路

需要定义个外部新链表存储新生成的链表,while里面有四步操作:
首先外部定义个pre = None来承接新的链表
1.定义tmp = pHead.next,将当前节点之后的节点先存起来
2.将当前节点反转,指向外部定义pre变量:pHead.next = pre
3.更新外部pre,让pre存储最新节点:pre = pHead
4.当前节点已经反转完毕,开始下一个节点:pHead = pHead.next

2.具体算法

反转链表:输入一个链表,反转链表后,输出新链表的表头。

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
class Solution:
    # 返回ListNode
    def ReverseList(self, head):
        # write code here
        if(head == None or head.next == None):
            return head
        pre = None
        while head:
            tmp = head.next
            head.next = pre
            pre = head
            head = tmp
        return pre


题目2、链表是否有环
1.整体思路

有两种思路:

  • 1.如果申请辅助空间,申请一个数组,循环每个节点放到数组中,如果新的节点已经在节点中了,说明有环
  • 2.扩展:你能给出空间复杂度O(1)的解法么:
    用两个指针,一个快指针,一个慢指针,快指针每次比慢指针多走一步,如果他们两个会相遇就有环
2.具体算法

(1)方法一:剑指offer:55题
题目:链表中环的入口结点----给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
class Solution:
    def EntryNodeOfLoop(self, pHead):
        # write code here
        node = []
        if not pHead:
            return None
        while pHead:
            if pHead in node:
                return pHead
            else:
                node.append(pHead)
                pHead = pHead.next

方法二:
nowcoder 在线编程:top5
题目:链表中环的入口结点----给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。你能给出空间复杂度O(1)的解法么


class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def hasCycle(self , head ):
        # write code here
        p = head
        q = head
        while(p!=None and p.next!=None):
            p = p.next.next
            q = q.next
            if p == q:
                return True
        return False
题目3、链表是否有环,并返回环的节点
1.整体思路
2.代码
class Solution:
    def detectCycle(self , head ):
        # write code here
        if  head is None or head.next is None:
            return None
        f = head
        s = head
        while(s!=None and f.next!=None):
            f = f.next.next
            s = s.next
            if s is None or f is None:
                return None
            if s == f:     ####找到指针相交点,但是不一定是入口节点
                s = head   #### 让慢指针从新从开始节点走,快慢节点相交的位置,就是入口节点 
                while s is not f:
                    s = s.next
                    f = f.next
                return s
        return None
题目4、合并两个有序链表

来源:剑指offer第25题

1.整体思路

(1)因为两个链表都是有序的,所以每次只需要比较两个链表头结点大小就行了
(2)较小的值需要三步操作:a.把当前节点(指针)赋给新头结点的next b.当前节点指针下移动 c.新节点指针下移
(3)对最终剩余的链表处理,直接拼接赋值即可

2.代码
# 写法1
class Solution:
    # 返回合并后列表
    def Merge(self, pHead1, pHead2):
        # write code here
        p = pHead = ListNode(0)
        while pHead1 and pHead2:
            if pHead1.val<pHead2.val:
                pHead.next = pHead1
                pHead1 = pHead1.next
                pHead = pHead.next
            else:
                pHead.next = pHead2
                pHead2 = pHead2.next
                pHead = pHead.next
        if pHead1:
            pHead.next = pHead1
        if pHead2:
            pHead.next = pHead2
        return p.next

# 写法2:
class ListNode:
     def __init__(self, x):
         self.val = x
         self.next = None


class Solution:
    def Merge(self , pHead1: ListNode, pHead2: ListNode) -> ListNode:
        # write code here
        head = p = ListNode(0)
        while pHead1 and pHead2:
            if pHead1.val<pHead2.val:
                p.next = pHead1
                pHead1 = pHead1.next
            else:
                p.next = pHead2
                pHead2 = pHead2.next
            p = p.next 
        p.next = pHead1 or pHead2
        return head.next

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值