目录
题目一:合并两个有序链表
代码:
题目二:合并k个有序链表
代码:
题目三:判断链表中是否有环
代码:
题目四:链表中环的入口结点
代码:
题目一:合并两个有序链表
代码:
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
public ListNode Merge (ListNode pHead1, ListNode pHead2) {
if(pHead1==null) return pHead2;
if(pHead2==null) return pHead1;
// write code here
ListNode result=null;
ListNode index1=pHead1;
ListNode index2=pHead2;
if(index1.val<index2.val){
result=index1;
index1=index1.next;
}else{
result=index2;
index2=index2.next;
}
ListNode last=result;
while(index1!=null && index2!=null){
if(index1.val<index2.val){
last.next=index1;
index1=index1.next;
}else{
last.next=index2;
index2=index2.next;
}
last=last.next;
}
if(index1==null){
last.next=index2;
}
if(index2==null){
last.next=index1;
}
return result;
}
}
题目二:合并k个有序链表
代码:
利用的是“归并排序”的思路。
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param lists ListNode类ArrayList
* @return ListNode类
*/
public ListNode mergeKLists (ArrayList<ListNode> lists) {
// write code here
return divideMerge(lists,0,lists.size()-1);
}
//划分合并区间
public ListNode divideMerge(ArrayList<ListNode> lists,int left,int right){
if(left>right) return null;
else if(left==right) return lists.get(left);
else{
int mid=(left+right)/2;
return Merge(divideMerge(lists,left,mid),divideMerge(lists,mid+1,right));
}
}
//合并两个有序链表
public ListNode Merge (ListNode pHead1, ListNode pHead2) {
if (pHead1 == null) return pHead2;
if (pHead2 == null) return pHead1;
// write code here
ListNode result = null;
ListNode index1 = pHead1;
ListNode index2 = pHead2;
if (index1.val < index2.val) {
result = index1;
index1 = index1.next;
} else {
result = index2;
index2 = index2.next;
}
ListNode last = result;
while (index1 != null && index2 != null) {
if (index1.val < index2.val) {
last.next = index1;
index1 = index1.next;
} else {
last.next = index2;
index2 = index2.next;
}
last = last.next;
}
if (index1 == null) {
last.next = index2;
}
if (index2 == null) {
last.next = index1;
}
return result;
}
}
题目三:判断链表中是否有环
代码:
利用的是双指针(快慢指针)的思路。
import java.util.*;
/**
* 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;
ListNode low=head;
while(fast!=null && fast.next!=null){
fast=fast.next.next;
low=low.next;
if(fast==null) return false;
if(fast==low) return true;
}
return false;
}
}
题目四:链表中环的入口结点
代码:
快慢指针的思路
import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead) {
if (pHead == null || pHead.next == null) {
return null;
}
ListNode fast = pHead;
ListNode low = pHead;
while (fast != null && fast.next != null) {
fast = fast.next.next;
low = low.next;
if (fast == low) break;
}
if(fast==null) return null;
low=pHead;
while(low != fast){
fast=fast.next;
low=low.next;
}
return fast;
}
}