链表——面试必刷TOP101

链表

BM1 反转链表

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }

}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
        ListNode pre=null,cur=head;
        while(cur!=null){
            ListNode tmp=cur.next;
            cur.next=pre;
            pre=cur;
            cur=tmp;
        }
        return pre;
    }
}

public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null||head.next==null) return head;
        ListNode cur=ReverseList(head.next);
        head.next.next=head;
        head.next=null;
        return cur;
    }
}

BM2 链表内指定区间反转

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {

    public ListNode reverseBetween (ListNode head, int m, int n) {
        ListNode dum=new ListNode(0);
        dum.next=head;
        ListNode g=dum,p=head;
        for(int i=0;i<m-1;i++){
            p=p.next;
            g=g.next;
        } 
        for(int i=0;i<n-m;i++){
            ListNode removed=p.next;
            p.next=p.next.next;
            removed.next=g.next;
            g.next=removed;
        }
        return dum.next;
    }
}

BM3 链表中的节点每k个一组翻转

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {

    public ListNode reverseKGroup (ListNode head, int k) {
        ListNode dum=new ListNode(0);
        dum.next=head;
        ListNode pre=dum,end=dum;
        while(end.next!=null){
            for(int i=0;i<k && end!=null;i++) end=end.next;
            if(end==null) break;
            ListNode second=end.next;
            end.next=null;
            ListNode start=pre.next;
            pre.next=reverseList(start);
            start.next=second;
            pre=start;
            end=start;
        }
        return dum.next;
    }
    private ListNode reverseList(ListNode head){
        ListNode pre=null,cur=head;
        while(cur!=null){
            ListNode tmp=cur.next;
            cur.next=pre;
            pre=cur;
            cur=tmp;
        }
        return pre;
    }
}

BM4 合并两个排序的链表

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode l1,ListNode l2) {
        ListNode dum=new ListNode(0),cur=dum;
        while(l1!=null && l2!=null){
            if(l1.val<=l2.val){
                cur.next=l1;
                l1=l1.next;
            }else{
                cur.next=l2;
                l2=l2.next;
            }
            cur=cur.next;
        }
        cur.next=l1!=null?l1:l2;
        return dum.next;
    }
}

BM5 合并k个已排序的链表

import java.util.*;
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode mergeKLists(ArrayList<ListNode> lists) {
       return merge(lists,0,lists.size()-1);
    }
    private ListNode merge(ArrayList<ListNode> lists,int l,int r){
        if(l==r)return lists.get(l);
        if(l>r) return null;
        int m=(l+r)/2;
        return mergeTwoList(merge(lists,l,m),merge(lists,m+1,r));
    }
     public ListNode mergeTwoList(ListNode l1,ListNode l2){
        ListNode dum=new ListNode(0),cur=dum;
        while(l1!=null&&l2!=null){
            if(l1.val<=l2.val){
                cur.next=l1;
                l1=l1.next;
            }else{
                cur.next=l2;
                l2=l2.next;
            }
            cur=cur.next;
        }
        cur.next=l1==null?l2:l1;
        return dum.next;
    }
}

BM6 判断链表中是否有环

/**
 * 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) {
        if(head==null||head.next==null) return false;
        ListNode fast=head.next,slow=head;
        while(fast!=slow){
            if(fast==null||fast.next==null) return false;
            fast=fast.next.next;
            slow=slow.next;
        }
        return true;
    }
}

BM7 链表中环的入口结点

/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {

    public ListNode EntryNodeOfLoop(ListNode head) {
        ListNode fast=head,slow=head;
        while(fast!=null && fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(slow==fast) break;
        }
        if(fast==null||fast.next==null) return null;
        fast=head;
        while(slow!=fast){
            slow=slow.next;
            fast=fast.next;
        }
        return fast;
    }
}

BM8 链表中倒数最后k个结点

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {

    public ListNode FindKthToTail (ListNode head, int k) {
        ListNode first=head,second=head;
        for(int i=0;i<k;i++){
            if(first==null) return null;
            first=first.next;
        } 
        while(first!=null){
            first=first.next;
            second=second.next;
        }
        return second;
    }
}

BM9 删除链表的倒数第n个节点

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {

    public ListNode removeNthFromEnd (ListNode head, int k) {
        ListNode dum=new ListNode(0);
        dum.next=head;
        ListNode first=head,second=dum;
        for(int i=0;i<k;i++){
            if(first==null) return null;
            first=first.next;
        }
        while(first!=null){
            first=first.next;
            second=second.next;
        }
        second.next=second.next.next;
        return dum.next;
    }
}

BM10 两个链表的第一个公共结点

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindFirstCommonNode(ListNode p1, ListNode p2) {
        ListNode head1=p1,head2=p2;
        while(p1!=p2){
            p1=p1!=null?p1.next:head2;
            p2=p2!=null?p2.next:head1;
        }
        return p1;
    }
}

BM11 链表相加(二)

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {

    public ListNode addInList(ListNode l1, ListNode l2) {
        Deque<Integer> stack1 = new LinkedList<>();
        Deque<Integer> stack2 = new LinkedList<>();
        while (l1 != null) {
            stack1.push(l1.val);
            l1 = l1.next;
        }
        while (l2 != null) {
            stack2.push(l2.val);
            l2 = l2.next;
        }
        int carry = 0;
        ListNode res = null;
        while (!stack1.isEmpty() || !stack2.isEmpty() || carry != 0) {
            int a = stack1.isEmpty() ? 0 : stack1.pop();
            int b = stack2.isEmpty() ? 0 : stack2.pop();
            int sum = a + b + carry;
            carry = sum / 10;
            ListNode curnode = new ListNode(sum%10);
            curnode.next = res;
            res = curnode;
        }
        return res;
    }
}

BM12 单链表的排序

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {

    public ListNode sortInList(ListNode head) {
        if(head==null||head.next==null) return head;
        ListNode fast=head,slow=head;
        while(fast.next!=null&&fast.next.next!=null){
            slow=slow.next;
            fast=fast.next.next;
        }
        ListNode l2=slow.next;
        slow.next=null;
        ListNode l1=sortInList(head);
        l2=sortInList(l2);

        ListNode dum=new ListNode(0),cur=dum;
        while(l1!=null&&l2!=null){
            if(l1.val<l2.val){
                cur.next=l1;
                l1=l1.next;
            }else{
                cur.next=l2;
                l2=l2.next;
            }
            cur=cur.next;
        }
        cur.next=l1!=null?l1:l2;
        return dum.next;
    }
}

BM13 判断一个链表是否为回文结构

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {

    public boolean isPail (ListNode head) {
        ListNode fast=head,slow=head;
        while(fast.next!=null&&fast.next.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        ListNode l2=slow.next;
        slow.next=null;
        l2=reverseList(l2);
        while(head!=null&&l2!=null){
            if(head.val!=l2.val) return false;
            head=head.next;
            l2=l2.next;
        }
        slow.next=reverseList(l2);
        return true;
    }
    private ListNode reverseList(ListNode head){
        ListNode pre=null,cur=head;
        while(cur!=null){
            ListNode tmp=cur.next;
            cur.next=pre;
            pre=cur;
            cur=tmp;
        }
        return pre;
    }
}

BM14 链表的奇偶重排

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {

    public ListNode oddEvenList (ListNode head) {
        if(head==null) return null;
        ListNode oddHead=head,evenHead=head.next;
        ListNode odd=head,even=head.next;
        while(even != null && even.next != null){
            odd.next=even.next;
            odd=odd.next;
            even.next=odd.next;
            even=even.next;
        }
        odd.next=evenHead;
        return oddHead;
    }
}

BM15 删除有序链表中重复的元素-I

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {

    public ListNode deleteDuplicates (ListNode head) {
        if(head==null) return null;
        ListNode cur=head;
        while(cur!=null && cur.next != null){
            if(cur.val==cur.next.val) cur.next=cur.next.next;
            else cur=cur.next;
        }
        return head;
    }
}

BM16 删除有序链表中重复的元素-II

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {

    public ListNode deleteDuplicates (ListNode head) {
        ListNode dum=new ListNode(0),cur=dum;
        dum.next=head;
        while(cur.next!=null && cur.next.next!=null){
            if(cur.next.val==cur.next.next.val){
                int x=cur.next.val;
                while(cur.next != null && cur.next.val==x)cur.next=cur.next.next;
            }else cur=cur.next;
        }
        return dum.next;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值