labuladong算法小抄-链表双指针-leetcode2、leetcode19、leetcode21、leetcode23、leetcode25、83、92、141、142、160、234、876

1.链表双指针

1.1 思想

用双指针模拟链表比遍历

2.例题

 leetcode2

 

/**
 * 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 addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode res = new ListNode();
        res.next = null;
        ListNode tail = res;
        boolean jinwei = false;
        while(l1!=null||l2!=null){
            if(l1==null){
                int tep = l2.val;
                if(jinwei){
                    tep++;
                    jinwei = false;
                }
                if(tep >= 10){
                    jinwei = true;
                    tep %=10;
                }
                ListNode temp = new ListNode(tep);
                temp.next = null;
                tail.next = temp;
                tail = temp;

                l2=l2.next;
            }else if(l2==null){
                int tep = l1.val;
                if(jinwei){
                    tep++;
                    jinwei = false;
                }
                if(tep >= 10){
                    jinwei = true;
                    tep %=10;
                }
                ListNode temp = new ListNode(tep);
                temp.next = null;
                tail.next = temp;
                tail = temp;
                 l1=l1.next;
            
            }else {
                int tep = l1.val+l2.val;
                if(jinwei){
                    tep++;
                    jinwei = false;
                }
                if(tep >= 10){
                    jinwei = true;
                    tep %=10;
                }
                ListNode temp = new ListNode(tep);
                temp.next = null;
                tail.next = temp;
                tail = temp;
                l1=l1.next;
                l2=l2.next;
            }
        }
         if(jinwei){
                ListNode temp = new ListNode(1);
                temp.next = null;
                tail.next = temp; 
                }
        return res.next;
    }
}

leetcode19

/**
 * 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 removeNthFromEnd(ListNode head, int n) {
        ListNode res = new ListNode();
        res.next = head;
        ListNode tail = res;
        ListNode h1 = res;
        while(n!=0){
            tail = tail.next;
            n--;
        }
        while(tail.next !=null){
            tail = tail.next;
            h1 = h1.next;
        }
        h1.next = h1.next.next;
        return res.next;
    }
}

 

leetcode21

/**
 * 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 list1, ListNode list2) {
        ListNode res = new ListNode();
        res.next = null;
        ListNode tail = res;
        while(list1!=null|list2!=null){
            if(list1==null){
                ListNode tep = list2;
                list2 = list2.next;
                tep.next =null;
                tail.next = tep;
                tail = tep;
            }else if(list2==null){
                 ListNode tep = list1;
                list1 = list1.next;
                tep.next =null;
                tail.next = tep;
                tail = tep;
            }else if(list1.val>list2.val){
                ListNode tep = list2;
                list2 = list2.next;
                tep.next =null;
                tail.next = tep;
                tail = tep;
            }else{
                 ListNode tep = list1;
                list1 = list1.next;
                tep.next =null;
                tail.next = tep;
                tail = tep;
            }
        }
        return res.next;
    }
}

 

leetcode23

/**
 * 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 mergeKLists(ListNode[] lists) {
        ListNode res = new ListNode();
        res.next =null;
        ListNode tail = res;
        boolean flag = true;
        int index = -1;
        while(flag){
          for (int i=0,ilen=lists.length;i<ilen;i++){
              if(lists[i]!=null){
                  if(index == -1){
                      index = i;
                  }else {
                      if(lists[i].val < lists[index].val){
                          index = i;
                      }
                  }
              }
          }
          if(index == -1){
              flag = false;
          }else {
              ListNode tep = lists[index];
              lists[index]=lists[index].next;
              tep.next =null;
              tail.next = tep;
              tail = tep;
          }
          index = -1;
        }
        return res.next;
    }
}

 

leetcode25

 

/**
 * 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 reverseKGroup(ListNode head, int k) {
        ListNode res = new ListNode();
        res.next = null;
        ListNode tail = res;
        // 双指针
        ListNode p1 = head;
        ListNode p2 = head;
        // 记录
        int cv = 1;
        if(p1==null)cv =0;
        while(p2!=null){
            if(cv == k){
                head = p2.next;
                p2.next = null;
                ListNode tep= reverse(p1);
                tail.next = tep;
                tail = p1;
                p1 = head;
                p2 = head;
                if(p1 == null)cv=0;
                else cv =1;
            }
            if(p2 == null)break;
            p2=p2.next;
            cv ++;
        }
        if(cv!=0){
            tail.next = p1;
        }
        return res.next;
    }
    public ListNode reverse(ListNode head){
        ListNode res = new ListNode();
        res.next = null;
        while(head!=null){
            ListNode tep = head;
            head=head.next;
            tep.next = res.next;
            res.next = tep;
        }
        return res.next;
    }
}

leetcode83

/**
 * 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 deleteDuplicates(ListNode head) {
        HashSet<Integer> set = new HashSet<>();
        ListNode res =new ListNode();
        res.next = head;

        ListNode h1=res,h2=head;
        while(h2!=null){
            if(set.contains(h2.val)){
                h1.next = h2.next;
                h2=h1.next;
            }else{
                set.add(h2.val);
                   h1=h1.next;
                   h2=h2.next;
            }
        }
        return res.next;
    }
}

 

leetcode92

/**
 * 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 reverseBetween(ListNode head, int left, int right) {
        ListNode res = new ListNode();
        res.next = head;
        ListNode p1=res,p2=null,p3=null,p4=null;
        int index =1;
        while(head!=null){
            if(index == left -1){
                p1 = head;
            }
            if(index == left){
                p2 = head;
            }
            if(index == right){
                p3 = head;
                p4 = head.next;
            }
            index++;
            head=head.next;
        }
        
        p3.next = null;
        reverse(p2);
        p1.next = p3;
        p2.next = p4;
        return res.next;
    }
    public ListNode reverse(ListNode head){
        ListNode res =new ListNode();
        res.next = null;
        while(head!=null){
            ListNode tep = head;
            head=head.next;
            tep.next = res.next;
            res.next = tep;

        }
        return res.next;
    }
}

 

leetcode141

 

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        HashSet<ListNode> set = new HashSet<>();
        while(head!=null){
            if(set.contains(head)){
                return true;
            }
            else {
                set.add(head);
            }
            head= head.next;
        }
        return false;
    }
}

leetcode142

 

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        HashSet<ListNode> hashset = new HashSet<>();

        while(head!=null){
            if(hashset.contains(head)){
                return head;
            }else {
                hashset.add(head);
            }
            head=head.next;
        }
        return null;
    }
}

leetcode160

/**
 * 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) {
        int count = 0;
        ListNode h1 = headA;
        ListNode h2 = headB;
        while(headA!=null || headB != null){
            if(headB == headA) return headA;

            headA = headA.next;

            headB = headB.next;

            if(headA == null){
                headA = h2;
                count++;
            }
            if(headB == null){
                headB = h1;
                count++;
            }
            if(count == 2){
                break;
            }
        }
        while(headA!=null && headB != null){
            if(headB == headA) return headA;

            headA = headA.next;

            headB = headB.next;

            
        }
        return null;
    }
}

leetcode234

 

/**
 * 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) {
        int sum = 0;
        ListNode h1 = head;
        while(h1!=null){sum++;h1=h1.next;}
        int right = 0;
        if(sum%2==0){
            right = sum / 2 +1;
        }else{
            right = sum / 2 +2;
        }
        ListNode h2 = null;
        h1 = head;
        int index =1;
        while(h1 !=null){
            if(index == right){
                h2 = h1;
                break;
            }
            index++;
            h1=h1.next;
        }
        h2 = reverse(h2);
        h1 = head;
        while(h1!=null&&h2!=null){
            if(h1.val!=h2.val){
                return false;
            }
              h1=h1.next;
             h2=h2.next;
        }

        return true;
    }
    public ListNode reverse(ListNode head){
        ListNode res = new ListNode();
        res.next = null;
        while(head!=null){
            ListNode tep = head;
            head = head.next;
            tep.next = res.next;
            res.next = tep;
        }
        return res.next;
    }
}

leetcode876

 

/**
 * 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 middleNode(ListNode head) {
        ListNode h1 = head;
        ListNode h2 = head;
        if(head.next == null)return head;
        while(h1!=null&&h2!=null){
                h1 = h1.next;
                h2 = h2.next;
                if(h2.next==null){
                    return h1;
                }else{
                    h2 = h2.next;
                }
                if(h2.next == null)break;
        }
        return h1;

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿联爱学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值