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;
        }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LeetCode-Editor是一种在线编码工具,它提供了一个用户友好的界面编写和运行代码。在使用LeetCode-Editor时,有时候会出现乱码的问题。 乱码的原因可能是由于编码格式不兼容或者编码错误导致的。在这种情况下,我们可以尝试以下几种解决方法: 1. 检查文件编码格式:首先,我们可以检查所编辑的文件的编码格式。通常来说,常用的编码格式有UTF-8和ASCII等。我们可以将编码格式更改为正确的格式。在LeetCode-Editor中,可以通过界面设置或编辑器设置来更改编码格式。 2. 使用正确的字符集:如果乱码是由于使用了不同的字符集导致的,我们可以尝试更改使用正确的字符集。常见的字符集如Unicode或者UTF-8等。在LeetCode-Editor中,可以在编辑器中选择正确的字符集。 3. 使用合适的编辑器:有时候,乱码问题可能与LeetCode-Editor自身相关。我们可以尝试使用其他编码工具,如Text Editor、Sublime Text或者IDE,看是否能够解决乱码问题。 4. 查找特殊字符:如果乱码问题只出现在某些特殊字符上,我们可以尝试找到并替换这些字符。通过仔细检查代码,我们可以找到导致乱码的特定字符,并进行修正或替换。 总之,解决LeetCode-Editor乱码问题的方法有很多。根据具体情况,我们可以尝试更改文件编码格式、使用正确的字符集、更换编辑器或者查找并替换特殊字符等方法来解决这个问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值