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

博客围绕链表展开,涵盖判断链表是否有环、删除链表节点、去除有序链表重复元素、找两链表交点、移除指定值元素、反转链表、合并两有序链表以及判断链表是否为回文等常见操作和问题的求解。

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

  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;
        }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值