链表常考习题分析汇总
//5.合并两个有序链表为一个有序链表,新链表是通过拼接给定两个链表中所有节点组成的。
public static void createCut(Node headA,Node headB){
headA.next = headB.next.next
}
public Node mergeTwoLists(Node headA,Node headB){
Node node = new Node(data:-1);//???
Node tmp = node;
while(headA != null && headB != null){
if(headA.data < headB.data){//将两个链表上的点一个个串起来
tmp.next = headA;
headA = headA.next;
tmp = tmp.next;//向前步径;
}else{
tmp.next = headB;
headB = headB.next;
tmp = tmp.next;//向前步径;
}
}
if(headA != null){//一个链表为空情况;
tmp.next = headA;
}
if(headB != null){//一个链表为空情况;
tmp.next = headB;
}
return node.next;
}
//6.编写代码,以给定值X为基准,将链表分割为两部分,所有小于X的结点排在大于或等于X的点之前。
public Node partition(int x){
Node beforeStart = null;
Node beforeEnd = null;
Node afterStart = null;
Node afterEnd = null;
Node cur = this.head;
while(cur != null){
Node curNext = cur.next;
cur.next = null;
if(cur.data < x){
if(beforeStart == null){
beforeEnd = cur;
beforeStart = cur;
}else{
beforeEnd.next = cur;
beforeEnd = cur;//将小于基点的cur插入最后,变成beforeEnd。
}
}else{//cur.data >= x;
if(afterStart = null){
afterEnd = cur;
afterStart = cur;
}else{
afterEnd.next = cur;
afterEnd = cur;
}
}
cur = curNext;
}
if(beforeStart == null){//全部串完后,小于X基点线上无数字,则返回大于X基点线段。
return afterStart;
}
beforeEnd.next = afterStart;
return beforeStart;
}
//7.在一个排序的链表中,存在重复的结点,请删除重复结点,返回头指针。
public Node deleteDuPublication(){
Node newHead = new Node(-1);//????
Node tmp = newHead;
Node cur = this.head;
while(cur != null){
if(cur.next != null && cur.data == cur.next.data){
while(cur.next != null && cur.data == cur.next.data){
cur = cur.next;//多走一步,走两步时,用相同条件while嵌套。
}
cur = cur.next;
tmp.next = cur;//让头指针跟着cur,保证地址不为null。
}else{
tmp.next = cur;
tmp = tmp.next;
cur = cur.next;
}
}
return newHead.next;
}
//8.链表的回文结构。
public class solution{
public ListNode detectcycle(ListNode head){
ListNode fast = head;
ListNode slow = head;
if(this.head == null){
return false;
}
if(this.head.next == null){
return true;
}
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if(fast = slow){
break;2
}
}
Node p = slow.next;
while(p != null){
Node pNext = p.next;
p.next = slow;//回文
slow = p;
p = pNext;
if(p != null){
pNext = p.next;
}
}
while(this.head != slow){//判断回文,两头相同往中间走
if(this.head,data != slow.data){
return false;
}
if(this.head.next = slow){//偶数判断
return ture;
}
this.head = this.head.next;
slow = slow.next;
}
return ture;
}
}
//9.输入两个链表,输出他们的公共结点。
public static Node getlaterSectionNode(Node headA,Node headB){
int lenA = 0;
int lenB = 0;
Node PL = headA;
Node PS = headB;
while(PL != null){
LenA ++;
PL = PL.next;
}
while(PS != null){
LenB ++;
PS = PS.next;
}
PL = headA;
PS = headB;
int Len = LenA - LenB;//找出两者链表的差值。
if(len < 0){
PL = headB;
PS = headA;
}
for(int i = 0;i < len;i++){
PL = PL.next;//让长的先走len步,保持后续长短相等
}
while(PL != null && PS != null && PL != PS){
pl = pl.next;//一人一步走,直到相交。
ps = ps.next;
}
if(pl == null || ps == null){
return null;
}
return PL;
}
//10.环形链表:给定一个链表,判断链表是否有环。
public boolean hasCycle(){
Node fast = this.head;
Node slow = this.head;
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if(fast == slow){
return ture;
}
}
return false;
}//创建链表环的代码:linkedlist.createCycle();
//11.给定一个链表,返回链表开始入环的第一个节点,如果链表无环,返回null。
public class solution{
public listNode detectCycle(listNode head){
listNode fast = this.head;
listNode slow = this.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 null;
}
fast = head;
while(fast != slow){
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
//4.输入一个链表,输出该链表中倒数第k个结点。
public class solution{
public listNode FindthToTail(listNode head,int k){
if (head == null || k == 0){//快慢指针
return null;
}
listNode fast = head;
listNode slow = head;
while(k-1 > 0){
if(fast.next != null){
fast = fast.next;//slow和fast相差k-1个身位
k–;
}else{
system.out.println(“没有这个结点!”);
return null;
}
}
while(fast.next != null){
fast = fast.next;
slow = slow.next;//slow位置即为倒数第k个位置
}
return slow;
}
}
//3.给定一个带有头结点的head非空单链表,打印链表中间的元素,如果中间有两个,返回第一个。
public listNode middleNode(listNode head){//第一种解法
if(head == null){
return null;
}
int len = size (head)/2;//找到中间元素,标记len。
listNode cur = head;
for(int i = 0;i < len;i++){
cur = cur.next;
}
return cur;
}
public listNode middleNode(listNode head){//第二种解法,快慢指针
if(head == null){//终止条件。
return null;
}
listNode fast = head;
listNode slow = head;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
//2.反转一个单链表。
public void reverse(){
if(this.head == null){
return;
}
Node cur = this.head.next;
Node prev = this.head;
Node nextcur = cur.next;
if(nextcur == cur){//null == null,说明只有一个元素,即cur就是唯一的链表点。
cur.next = prev;
prev.next = null;
this.head = cur;
}
while(cur.next != null){
this.head.next = null;
cur.next = prev;
prev = cur;
cur = nextcur;
nextcur = nextcur.next;
}
cur.next = prev;
this.head = cur;//cur作为移动点,最终成为head。
}
public Node reverselist(){
while(cur != null){
cur.next = prev;
prev = cur;
cur = curNext;
curNext = cur.next;
}else{
return cur;
}
}
//2.反转一个单链表。
class Solution{
public ListNode reverseList(ListNode head){
ListNode prev = null;
ListNode cur = head;
ListNode newHead = null;
while(cur != null){
ListNode curNext = cur.next;
if(curNext == null){
newHead = cur;
}
cur.next = prev;
prev = cur;
cur = curNext;
}
return newHead;
}
}
//1.删除链表中等于给定值"key"的所有节点。
public void removeAllkey(int key){
Node prev = this.head;
Node cur = this.head.next;//删除节点cur
while(cur != null){
if(cur.data = key){
prev.next = cur.next;//删除cur这个位置的元素
cur = cur.next;
}else{
prev = cur;
cur = cur.next;
}
}
if(head.data == key){
head = head.next;
}
}