LeetCode-链表

  1. 给定一个链表,删除链表的倒数第n个节点,并且返回链表的头结点
    解题思路:
    用2个距离为n的指针,分别向前移动,直到先移动的指针到达链表的尾部。
//scala
/**
 * Definition for singly-linked list.
 * class ListNode(var _x: Int = 0) {
 *   var next: ListNode = null
 *   var x: Int = _x
 * }
 */
object Solution {
    def removeNthFromEnd(head: ListNode, n: Int): ListNode = {
        var first = head
        var second = head
        
        if(first==null)
            return head.next
            
        for(i <- 1 to n)
            first = first.next
        while(first.next!=null){
            first = first.next
            second = second.next
        }
        second.next = second.next.next
        head
    }
}
//python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        first = head
        second = head

        while n > 0:
            first = first.next
            n -= 1

        if first==None:
            return head.next

        while first.next:
            first = first.next
            second = second.next
        second.next = second.next.next
        return head
  1. 将两个有序链表合并为一个新的有序链表并返回。新的链表是通过拼接给定的两个链表的所有节点组成的。
    解题思路:
    用2个引用分别指向两个链表,判断当前2个引用指向的节点的值,将较小的一方添加到新节点上,并向后移动一位;直到2个引用中有一个到达链表尾部,将另一个直接添加到新链表的尾部。
//scala
/**
 * Definition for singly-linked list.
 * class ListNode(var _x: Int = 0) {
 *   var next: ListNode = null
 *   var x: Int = _x
 * }
 */
object Solution {
    def mergeTwoLists(l11: ListNode, l22: ListNode): ListNode = {
        var l1:ListNode = l11
        var l2:ListNode = l22
        if(l1==null || l2==null){
            if(l1==null)
                return l2
            else
                return l1
        }else{
            var head:ListNode = null
            if(l1.x < l2.x){
                head = l1
                l1 = l1.next
            }else{
                head = l2
                l2 = l2.next
            }
            var tail = head
            while(l1!=null && l2!=null){
                if(l1.x < l2.x){
                    tail.next = l1
                    l1 = l1.next
                    tail = tail.next
                }else{
                    tail.next = l2
                    l2 = l2.next
                    tail = tail.next
                }
            }
            if(l1!=null)
                tail.next = l1
            if(l2!=null)
                tail.next = l2
            head
        }
    }
}
#python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        #判断是否有链表为空,如果有,则直接返回另一个
        if l1 ==None or l2 == None:
            if l1 == None:
                return l2
            else:
                return l1
        #如果没有链表为空
        else:
            if l1.val < l2.val:
                head = l1
                l1 = l1.next
            else:
                head = l2
                l2 = l2.next
            tail = head
            while l1!=None and l2 != None:
                if l1.val < l2.val:
                    tail.next = l1
                    l1 = l1.next
                    tail = tail.next
                else:
                    tail.next = l2
                    l2 = l2.next
                    tail = tail.next
            if l1!=None:
                tail.next = l1
            if l2!=None:
                tail.next = l2
        return head
  1. 翻转链表
    解题思路:
    头插法
/**
 * Definition for singly-linked list.
 * class ListNode(var _x: Int = 0) {
 *   var next: ListNode = null
 *   var x: Int = _x
 * }
 */
object Solution {
    def reverseList(head: ListNode): ListNode = {
        var curr_node = head
        var temp_node:ListNode=null;
        var result = new ListNode()
        while(curr_node!=null){
            temp_node = curr_node
            curr_node = curr_node.next
            temp_node.next = result.next
            result.next = temp_node
        }
        result.next
    }
}
  1. 判断链表是否有环,有的话找出环的起始位置
    解题思路:
    用2个引用,其中一个表示快引用,另一个表示慢引用。快引用每次走2个节点,慢引用每次走一个节点。如果快引用到达链表尾部,则表明没有环,如果快引用赶上了慢引用,则表示有环。这时候需要计算出环的大小,然后算出环的初始位置就是类似计算链表倒数第n个节点的位置。
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def detectCycle(self, head: ListNode) -> ListNode:
        pos = 0
        first = head
        second = head
        if first == None or first.next == None: 
            return None
        while first!=None and first.next!=None and first.next.next!=None:
            first = first.next.next
            second = second.next
            #判断是否有环
            if first == second:
                pos += 1
                #有环的话,计算出环的大小
                while first.next != second:
                    first = first.next
                    pos += 1
                break
        if pos == 0:
            return None
        else:
            first = head
            second = head
            #找出环的位置
            while pos > 0:
                first = first.next
                pos -= 1
            while first != second:
                first = first.next
                second = second.next
        return second
  1. 找出链表的中间节点
    解题思路:
    快引用和慢引用
/**
 * Definition for singly-linked list.
 * class ListNode(var _x: Int = 0) {
 *   var next: ListNode = null
 *   var x: Int = _x
 * }
 */
object Solution {
    def middleNode(head: ListNode): ListNode = {
        var fast = head
        var low = head
        while(fast!=null && fast.next!=null){
            low = low.next
            fast = fast.next.next
        }
        low
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值