
链表面试题
链表
永不言弃12345
年轻人总是会找借口说这个东西不是我所感兴趣的,所以我做不好是应该的。 但他们没有注意的是,你面对的事情中感兴趣的是总是少数,这就使得大多数你做事情的态度总是很懈怠,很消极,这使你变成了一个很懈怠得人。当你面对自己感兴趣的东西时,你发现你已经攥不急紧拳头了。
展开
-
找到两个链表相交的节点
public static void createCut(Node headA,Node headB){ headA.next=headB.next.next; } //输入两个链表,找到链表相交的节点 public static Node getInterSectionNode(Node headA,Node headB){ Node ps=headA;//默认指向长的单链表 Node pl=headB;//默认指向指向短的单原创 2020-06-26 18:55:07 · 198 阅读 · 0 评论 -
把两个有序单链表,合并为一个单链表
public static Node mergeTwoLists(Node headA,Node headB){ Node newHead=new Node(-1); Node tmp=newHead; while(headA!=null&&headB!=null){ if(headA.data<headB.data){ tmp.next=headA; .原创 2020-06-26 18:54:26 · 672 阅读 · 0 评论 -
找到链表环的入口
//找到环的入口 slow=this.head; slow=slow.next; fast=fast.next;public ListNode detectCycle() { ListNode fast = this.head; ListNode slow = this.head; while (fast != null && fast.next != null) { fast = fast.next.next; slow原创 2020-06-26 18:51:16 · 139 阅读 · 0 评论 -
链表是否有环
//链表是否有环 //走三步,走一步 很慢才能相遇 ,或者永远不相遇 public boolean hasCycle() { Node fast = this.head; Node slow = this.head; while (fast != null && fast.next != null) { //可以快速相遇 fast = fast.next.next;原创 2020-06-26 18:48:53 · 100 阅读 · 0 评论 -
链表是否是回文
//链表是否是回文 找到链表的中间节点 反转单链表 head从后往后走 slow从后往前走 public boolean chkPalindrome() { if (this.head == null){ return false; } if (this.head.next == null){ return true; } Node fast=this.head;原创 2020-06-26 18:48:13 · 97 阅读 · 0 评论 -
删除链表中重复的节点
//删除链表中重复的节点 public Node delete(){ Node newHead=new Node(-1); Node tmp=newHead; Node cur=this.head; while(cur!=null) { if (cur.next != null && cur.data == cur.next.data) {原创 2020-06-26 18:42:25 · 89 阅读 · 0 评论 -
以x为基准,将链表分成两部分,原来的数据顺序不变
/以x为基准,将链表分成两部分,原来的数据顺序不变 public Node partition(int x){ if(head==null){ return null; } Node cur=this.head; Node bs=null; Node be=null; Node as=null; Node ae=null; while(cur!=n原创 2020-06-26 18:41:50 · 184 阅读 · 0 评论 -
链表中倒数第K个节点
//链表中倒数第K各节点 k的合法性 public Node node(int k){ if(head==null){ return null; } if(k<=0){ System.out.println("K位置不合法"); return null; } Node fast=this.he原创 2020-06-26 18:40:07 · 73 阅读 · 0 评论 -
返回链表中间节点
//返回链表的中间结点,遍历一遍 public Node midNode(){ Node fast=this.head; Node slow=this.head; while(fast!=null&&fast.next!=null){ fast=fast.next.next; slow=slow.next; } return slow; }原创 2020-06-26 18:39:17 · 132 阅读 · 0 评论 -
反转单链表
//反转一个单链表 只需遍历一遍 public Node reverse () { Node cur=this.head;//待反转的节点 Node prev=null; Node newHead=null; while(cur!=null){ Node curNext=cur.next; if(curNext==null原创 2020-06-26 18:38:27 · 118 阅读 · 0 评论