20190730算法题存档

本文探讨了链表的多种高级操作,包括重新排序、检测循环、深复制及字符串的字典匹配。通过具体实例,如将链表1->2->3->4重新排序为1->4->2->3,以及实现leetcode字符串与字典['leet','code']的匹配,展示了算法设计与实现的精妙之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

 

Given a singly linked list L: L 0→L 1→…→L n-1→L n,
reorder it to: L 0→L nL 1→L n-1→L 2→L n-2→…

You must do this in-place without altering the nodes' values.

For example,
Given{1,2,3,4}, reorder it to{1,4,2,3}.

public class Solution {
     public void reorderList(ListNode head) {
        if(head == null || head.next == null) {
            return;
        }
        ListNode slow = head;
        ListNode quick = head;
        while(quick.next != null && quick.next.next != null) {
            slow = slow.next;
            quick = quick.next.next;
        }
        ListNode mid = slow.next;
        if(mid != null && mid.next != null) {
            ListNode next = mid.next;
            while(next != null) {
                mid.next = next.next;
                next.next = slow.next;
                slow.next = next;
                next = mid.next;
            }
        }
        ListNode p = head;
        ListNode q = slow.next;
        while(q != null && p != null){
            slow.next = q.next;
            q.next = p.next;
            p.next = q;
            p = p.next.next;
            q = slow.next;
        }
    }
}

题目描述

 

Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.

Follow up:
Can you solve it without using extra space?

public class Solution {
    public ListNode detectCycle(ListNode head) {
      if (head != null) {
            ListNode slow = head;
            ListNode fast = head;
            while (fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
                if (fast == slow) {
                    fast = head;
                    while (slow != fast) {
                        slow = slow.next;
                        fast = fast.next;
                    }
                    return slow;
                }
            }


        }

        return null;
    }
}

题目描述

 

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head != null) {
            ListNode slow = head;
            ListNode fast = head;
            while(fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
                if(fast == slow) {
                    return true;
                }
            }
        }

        return false;
    }
}

题目描述

 

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s ="leetcode",
dict =["leet", "code"].

Return true because"leetcode"can be segmented as"leet code".

public class Solution {
    public boolean wordBreak(String s, Set<String> dict) {
        boolean[] temp = new boolean[s.length() + 1];
        temp[0] = true;

        for(int i = 1; i < temp.length; i++) {
            for(int j = 0; j < i; j++) {
                if (temp[j] && dict.contains(s.substring(j, i))){
                    temp[i] = true;
                    break;
                }
            }
        }

        return temp[temp.length - 1];
    }
}

题目描述

 

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

public class Solution {
    public RandomListNode copyRandomList(RandomListNode head) {
        if(head == null || (head.next == null && head.random == null)){
            return head;
        }
        RandomListNode node = head;
        while(node != null){
            RandomListNode copy = new RandomListNode(node.label);
            copy.next = node.next;
            copy.random = node.random;
            node.next = copy;
            node = node.next.next;
        }

        node = head.next;
        while(node != null){
            if(node.next != null){
                node.next = node.next.next;
            }
            if(node.random != null){
                node.random  = node.random.next;
            }
            node = node.next;
        }

        return head.next;
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值