链表操作相关的题目

206. Reverse Linked List 

解法:新建节点 

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode p=new ListNode(0);
        p.next=null;
        while(head!=null){
            ListNode temp=head;
            head=head.next;
            temp.next=p.next;
            p.next=temp;
        }
        return p.next;
    }
}

92. Reverse Linked List II 

解法:新建节点+记录首尾节点

class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode p0=new ListNode(0);
        p0.next=head;
         ListNode p=p0;
        int count=1;
        while(count<m){
            p=p.next;
            count++;
        }
        count=n-m+1;
        ListNode q=new ListNode(0);
        ListNode start=p,tail=p.next,t=p.next;
        q.next=null;
        while(count>0){
            count--;
            ListNode temp=t;
            t=t.next;
            temp.next=q.next;
            q.next=temp;
        }
        start.next=q.next;
        tail.next=t;
        return p0.next;
    }
}

 

 

Remove Nth Node From End of List

解法:快慢指针+前缀节点(为了处理删除的是头节点

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode p=new ListNode(0);
        p.next=head;
        ListNode q=p;
        while(n>0){
            q=q.next;
            n--;
        }
        ListNode low=p;
        while(q.next!=null){
            q=q.next;
            low=low.next;
        }
        ListNode temp=low.next;
        low.next=temp.next;
        temp.next=null;
        return p.next;
    }
}

92. Reverse Linked List II 

解法:大问题分解成小问题;首先将链表分成前后两部分,然后对后半部分进行反转操作,然后合并。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public void reorderList(ListNode head) {
        if(head==null || head.next==null)
            return ;
        ListNode t1=head;
        int len=0;
        while(t1!=null){
            t1=t1.next;
            len++;
        }
        int mid=(len+1)/2;
        ListNode p0=new ListNode(0);
        t1=head;
        int count=1;
        while(count<mid){
            count++;
            t1=t1.next;
        }
        
       ListNode p1=head,p=p0;
        ListNode q1=t1.next;
        t1.next=null;
        ListNode q2=new ListNode(0);
        q2.next=null;
        while(q1!=null){
            ListNode temp=q1;
            q1=q1.next;
            temp.next=q2.next;
            q2.next=temp;
        }
        q1=q2.next;
        while(p1!=null && q1!=null){
            ListNode temp=p1;
            p1=p1.next;
            temp.next=null;
            p.next=temp;
            p=p.next;
            temp=q1;
            q1=q1.next;
            temp.next=null;
            p.next=temp;
            p=p.next;
        }
        if(p1!=null)
            p.next=p1;
        else if(q1!=null)
            p.next=q1;
        head=p0.next;
    }
}

86. Partition List 

解法:双链+合并

class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode small=new ListNode(0);
        small.next=null;
        ListNode big=new ListNode(0);
        big.next=null;
        ListNode s=small,b=big;
        while(head!=null){
            ListNode temp=head;
            head=head.next;
            temp.next=null;
            if(temp.val<x){
                s.next=temp;
                s=s.next;
            }else{
                b.next=temp;
                b=b.next;
            }
        }
        s.next=big.next;
        return small.next;
    }
}

23. Merge k Sorted Lists 

解法:分治+递归+合并

class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        return merge(lists,0,lists.length-1);
    }
    ListNode merge(ListNode lists[],int s,int e){
        if(e>s){
            int mid=(s+e)/2;
            ListNode low=merge(lists,s,mid);
            ListNode high=merge(lists,mid+1,e);
            ListNode p=new ListNode(0);
            ListNode p1=p;
            p1.next=null;
            while(low!=null && high!=null){
                if(low.val<high.val){
                    p1.next=low;
                    low=low.next;
                    p1=p1.next;
                    p1.next=null;
                }else{
                    p1.next=high;
                    high=high.next;
                    p1=p1.next;
                    p1.next=null;
                }
            }
            p1.next=low==null?high:low;
            return p.next;
        }else if(s==e)
            return lists[s];
        else 
            return null;
    }
}

25. Reverse Nodes in k-Group 

解法:快慢指针+保存下次计数的首尾+链表反转

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if(k<=1)
            return head;
        ListNode p=new ListNode(0);
        p.next=head;
        ListNode fast=p,low=p;
        int count=0;
        while(fast.next!=null){
            fast=fast.next;
            count++;
            if(count==k){
                ListNode newstart=low.next;
                ListNode nextend=fast.next;
                count=0;
                
                ListNode s=new ListNode(0);
                s.next=null;
                ListNode t1=low.next;
                
                while(t1!=nextend){
                    ListNode temp=t1;
                    t1=t1.next;
                    temp.next=s.next;
                    s.next=temp;
                }
                low.next=s.next;
                newstart.next=nextend;
                
                low=newstart;
                fast=newstart;
            }
        }
        return p.next;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值