链表面试题

1.求环的入口点

 

/**

 * 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) {

        ListNode fast=head;

        ListNode slow=head;

        while(fast!=null&&fast.next!=null){

            slow=slow.next;

            fast=fast.next.next;

            if(fast==slow){

                break;

            }

        }

        if(fast==null||fast.next==null){

            return null;

        }

        fast=head;

        while(fast!=slow){

            

            fast=fast.next;

            slow=slow.next;

        }

        return fast;

    }

}

2、判断链表带环

快慢指针,即慢指针一次走一步,快指针一次走两步,两个指针从链表其实位置开始运行,如果链表带环则一定会在环中相遇,否则快指针率先走到链表的末尾

public class Solution {

    public boolean hasCycle(ListNode head) {

        

        ListNode fast=head;

        ListNode slow=head;

       

        while(fast!=null&&fast.next!=null){

            fast=fast.next.next;

            slow=slow.next;

            if(fast==slow){

                break;

            }

        }

        if(fast==null||fast.next==null){

            return false;

        }

        

        return true;

    } 

}

3、判定链表相交并求出交点 

 

/**

 * 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 pl=headA;

        ListNode ps=headB;

        int lena=0;

        int lenb=0;

        while(pl!=null){

            pl=pl.next;

            lena++;

        }

        while(ps!=null){

            ps=ps.next;

            lenb++;

        }

        int len=lena-lenb;

        pl=headA;

        ps=headB;

        if(len<0){

            pl=headB;

            ps=headA;

            len=lenb-lena;

        }

        while(len!=0){

           pl=pl.next;

            len--;

        }

        while(pl!=ps){

            pl=pl.next;

            ps=ps.next;

        }

        if(pl==null){

            return null;

        }

return pl;

    }

}

4、判定链表是否是回文

import java.util.*;



/*

public class ListNode {

  int val;

  ListNode next = null;



  ListNode(int val) {

    this.val = val;

  }

}*/

public class PalindromeList {

  public boolean chkPalindrome(ListNode A) {

 ListNode slow=A;

    ListNode fast=A;

    if(A==null||A.next==null){

      return true;

    }

    while(fast!=null&&fast.next!=null){

      fast=fast.next.next;

      slow=slow.next;

    }

    ListNode cur=slow.next;

    while(cur!=null){

      ListNode curnext=cur.next;

      cur.next=slow;

      slow=cur;

      cur=curnext;

    }

    while(A!=slow){

      if(A.val!=slow.val){

        return false;

      }

      if(A.next==slow){

        return true;

      }

        slow=slow.next;

        A=A.next;

         

    }

    return true;   

  }

}

5、删除链表重复节点

 

 

class Solution {

  public ListNode removeDuplicateNodes(ListNode head) {

    ListNode ob = head;

    while (ob != null) {

      ListNode oc = ob;

      while (oc.next != null) {

        if (oc.next.val == ob.val) {

          oc.next = oc.next.next;

        } else {

          oc = oc.next;

        }

      }

      ob = ob.next;

    }

    return head;

  }

}

 6、实现链表题目: 给定 x, 把一个链表整理成前半部分小于 x, 后半部分大于等于 x 的形式

 

import java.util.*;



/*

public class ListNode {

  int val;

  ListNode next = null;



  ListNode(int val) {

    this.val = val;

  }

}*/

public class Partition {

  public ListNode partition(ListNode pHead, int x) {

    if(pHead==null){

      return null;

    }

    ListNode bs=null;

    ListNode be=null;

    ListNode es=null;

    ListNode ee=null;

    ListNode cur=pHead;

    while(cur!=null){

    if(cur.val<x){

      if(bs==null){

        bs=cur;

        be=cur;

      }

      else{

        be.next=cur;

        be=be.next;

      }

    }

      else{

        if(es==null){

        es=cur;

        ee=cur;

        }

        else{

          ee.next=cur;

          ee=ee.next;

        }

      }

         cur=cur.next;

      }

      

    

  if(bs==null){

    return es;

  }

    be.next=es;

  if(es!=null){

    ee.next=null;

       

  }

    return bs;

  }  

  }

7、实现链表题目: 合并两个有序链表 

/**

 * 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 newnode=new ListNode(-1);

ListNode tmp=newnode;

while(list1!=null&&list2!=null){

if(list1.val<list2.val){

    tmp.next=list1;

    tmp=tmp.next;

    list1=list1.next;



}

else{

    tmp.next=list2;

    tmp=tmp.next;

    list2=list2.next;

}

}

if(list1!=null){

    tmp.next=list1;

}

if(list2!=null){

    tmp.next=list2;

}

return newnode.next;

    }

}

 8、实现链表题目: 获取链表倒数第K个节点

/*

public class ListNode {

  int val;

  ListNode next = null;



  ListNode(int val) {

    this.val = val;

  }

}*/

public class Solution {

  public ListNode FindKthToTail(ListNode head,int k) {

if(k<=0||head==null){

  return null;

}

    ListNode fast=head;

    ListNode slow=head;

    while(k-1>0){

      if(fast.next==null){

        return null;

      }

      fast=fast.next;

      k--;

    }

    while(fast.next!=null){

      fast=fast.next;

      slow=slow.next;

    }

    return slow;

  }

}

 9、实现链表题目: 获取链表的中间节点

class Solution {

    public ListNode middleNode(ListNode head) {

        if(head==null){

            return null;

        }

ListNode fast=head;



ListNode slow=head;

while(fast!=null&&fast.next!=null){

    fast=fast.next.next;

    slow=slow.next;

}

return slow;

    }

}

 10、链表逆置

在遍历链表时,将当前节点的 \textit{next}next 指针改为指向前一个节点。由于节点没有引用其前一个节点,因此必须事先存储其前一个节点。在更改引用之前,还需要存储后一个节点。最后返回新的头引用。

/**

 * 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){

            return null;

        }

        if(head.next==null){

            return head;

        }

ListNode slow=head;

ListNode cur=head.next;

head.next=null;

while(cur!=null){

    ListNode fast=cur.next;

    cur.next=slow; 

    slow=cur;

    cur=fast;

   



}

return slow;

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值