Leetcode-linkedlist-easy (141,237,83,160,203,206,207,234)

链表常见操作及问题求解
博客围绕链表展开,涵盖判断链表是否有环、删除链表节点、去除有序链表重复元素、找两链表交点、移除指定值元素、反转链表、合并两有序链表以及判断链表是否为回文等常见操作和问题的求解。
  1. Linked List Cycle
    Given head, the head of a linked list, determine if the linked list has a cycle in it.
    There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a parameter.

Return true if there is a cycle in the linked list. Otherwise, return false.

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution1 {
    public boolean hasCycle(ListNode head) {
        if(head == null){
            return false;
        }
        ListNode fast=head;
        ListNode slow=head;
        while(fast !=null && fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){return true;} 
        }
        return false;
        
    }
}
//'Floyd's Tortoise and Hare', official answer
public class Solution2 {
    public boolean hasCycle(ListNode head) {
        if(head == null){
            return false;
        }
        ListNode fast=head.next;
        ListNode slow=head;
        while(fast!=slow){
            if(fast==null || fast.next==null){return false;} 
            fast=fast.next.next;
            slow=slow.next;
        }
        return true;
    }
}
  1. Delete Node in a Linked List
    Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly.
    It is guaranteed that the node to be deleted is not a tail node in the list.
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public void deleteNode(ListNode node) {
        if(node == null){return;}
        node.val=node.next.val;
        node.next=node.next.next;
    }
}
  1. Remove Duplicates from Sorted List:
    Given a sorted linked list, delete all duplicates such that each element appear only once.
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution1 {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode current = head;
        while(current!=null && current.next!=null){
            if(current.val==current.next.val){
                current.next=current.next.next;
            }else{
                current=current.next;
            }
        }
        return head;
    }
}
//faster solution
class Solution2 {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null){
            return null;
        }
        
        ListNode prev = head;
        ListNode current = head.next;
        while(current!=null){
            if(prev.val==current.val){
                prev.next=current.next;
            }else{
                prev=current;
            }
            current=current.next;
        }
        return head;
    }
}
  1. Intersection of Two Linked Lists
    Write a program to find the node at which the intersection of two singly linked lists begins.
    For example, the following two linked lists:
A:          a1 → a2
                      ↘
                          c1 → c2 → c3
                      ↗            
B:     b1 → b2 → b3

begin to intersect at node c1.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null || headB==null){
            return null;
        }
        ListNode a = headA;
        ListNode b = headB;
        
        while(a!=b){
            a = a==null? headB : a.next;
            b = b==null? headA : b.next;
        }
        return a;
    }
}
  1. Remove Linked List Elements
    Remove all elements from a linked list of integers that have value val.
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head==null){
            return null;
        }
        ListNode p = head;
        while(p!=null && p.val==val){
                p=p.next;
                head=p;
            }
        while(p!=null){
            if(p.next!=null && p.next.val==val){
                p.next=p.next.next;
            }else{
                p=p.next;
            }
        }
        return head;
    }
}
//similar speed, but it's pretty neat code to use fakehead
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode fakehead = new ListNode(-1);
        fakehead.next = head;
        ListNode current=head, prev=fakehead;
        while(current!=null){
            if(current.val==val){
                prev.next=current.next;
            }else{
                prev=prev.next;
            }
            current=current.next;
        }
        return fakehead.next;
        
    }
}
  1. Reverse Linked List
    Reverse a singly linked list.
    Example:
    Input: 1->2->3->4->5->NULL
    Output: 5->4->3->2->1->NULL
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null || head.next==null){
            return head;
        }
        ListNode a=head;
        ListNode b=head.next;
        a.next=null;
        while(b!=null){
            ListNode tmp=b.next;
            b.next=a;
            a=b;
            b=tmp;
        }
        return a;
    }
}
  1. Merge Two Sorted Lists
    Input: l1 = [1,2,4], l2 = [1,3,4]
    Output: [1,1,2,3,4,4]
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null) return l2;
        if(l2==null) return l1;
        
        ListNode result,p;
        
        if(l1.val<l2.val){
            result = l1;
            l1=l1.next;
        }else{
            result=l2;
            l2=l2.next;
        }
        
        p=result;
        while(l1!=null&&l2!=null){
            if(l1.val<l2.val){
                p.next=l1;
                l1=l1.next;
            }else{
                p.next=l2;
                l2=l2.next;
            }
            p=p.next;
        }
        if(l1!=null){
            p.next=l1;
        }else{
            p.next=l2;
        }
        return result;
    }
}
//思路类似,写法更简洁,注意一开始新建的那个node不在结果里
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode result=new ListNode();
        ListNode p=new ListNode();
        p=result;
        
        while(l1!=null && l2!=null){
            if(l1.val<l2.val){
                p.next=new ListNode(l1.val);                
                l1=l1.next;
            }else{
                p.next=new ListNode(l2.val);        
                l2=l2.next;
            }
            p=p.next;
        }
        if(l1!=null){
            p.next=l1;
        }else{
            p.next=l2;
        }
        return result.next;
    }
}
  1. Palindrome Linked List
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {
        ListNode leftList = head;
        ListNode midNode = head;
        int len=calculateListLength(head);
        int mid= len%2==0? len/2 : len/2+1;
        while(mid>0){
            midNode=midNode.next;
            mid-=1;
        }
        ListNode rightList=reverseList(midNode);
        int compareCount=len/2;
        while(compareCount>0){
            if(leftList.val != rightList.val){
                return false;
            }
            leftList=leftList.next;
            rightList=rightList.next;
            compareCount-=1;
        }
        return true;
      }  
    
        private int calculateListLength(ListNode head){
            int count=0;
            while(head!=null){
                count+=1;
                head=head.next;
            }
            return count;
        }
        private ListNode reverseList(ListNode head){
            ListNode a=null;
            ListNode b=head;
            while(b!=null){
                ListNode tmp=b.next;
                b.next=a;
                a=b;
                b=tmp;
            }
            return a;
        }
}
提供了基于BP(Back Propagation)神经网络结合PID(比例-积分-微分)控制策略的Simulink仿真模型。该模型旨在实现对杨艺所著论文《基于S函数的BP神经网络PID控制器及Simulink仿真》中的理论进行实践验证。在Matlab 2016b环境下开发,经过测试,确保能够正常运行,适合学习和研究神经网络在控制系统中的应用。 特点 集成BP神经网络:模型中集成了BP神经网络用于提升PID控制器的性能,使之能更好地适应复杂控制环境。 PID控制优化:利用神经网络的自学习能力,对传统的PID控制算法进行了智能调整,提高控制精度和稳定性。 S函数应用:展示了如何在Simulink中通过S函数嵌入MATLAB代码,实现BP神经网络的定制化逻辑。 兼容性说明:虽然开发于Matlab 2016b,但理论上兼容后续版本,可能会需要调整少量配置以适配不同版本的Matlab。 使用指南 环境要求:确保你的电脑上安装有Matlab 2016b或更高版本。 模型加载: 下载本仓库到本地。 在Matlab中打开.slx文件。 运行仿真: 调整模型参数前,请先熟悉各模块功能和输入输出设置。 运行整个模型,观察控制效果。 参数调整: 用户可以自由调节神经网络的层数、节点数以及PID控制器的参数,探索不同的控制性能。 学习和修改: 通过阅读模型中的注释和查阅相关文献,加深对BP神经网络与PID控制结合的理解。 如需修改S函数内的MATLAB代码,建议有一定的MATLAB编程基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值