
思路:
具体如下:
public class RemoveAllVal {
static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;
void removeAllVal(int val) {
if (head == null) return;
ListNode cur = head.next;
ListNode prev = head;
while (cur != null) {
if (cur.val == val) {
prev.next = cur.next;
cur = cur.next;
} else {
prev = cur;
cur = cur.next;
}
}
if (head.val == val) {
head = head.next;
}
}
}思路:
具体如下:
public class Reverse {
static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;
void reverse() {
if (head == null) return;
ListNode cur = head.next;
head.next = null;
while (cur != null) {
ListNode latter = cur.next;
cur.next = head;
head = cur;
cur = latter;
}
}
}思路:
具体如下:
public class MiddleNode {
static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;
ListNode middleNode() {
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;
}
}思路:
public class ChkPalindrome {
static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;
public boolean chkPalindrome() {
if (head == null) return true;
ListNode fast = head;
ListNode slow = head;
while (fast!=null && fast.next!=null) { //找到中间节点
fast = fast.next.next;
slow = slow.next;
}
ListNode cur = slow.next;
while (cur != null) { //反转后半部分
ListNode next = cur.next;
cur.next = slow;
slow = cur;
cur = next;
}
while (head != slow) { //判断回文
if (head.val != slow.val) return false;
if (head.next == slow) return true; //节点个数为偶数个时的处理
head = head.next;
slow = slow.next;
}
return true;
}
}思路:
具体如下:
public class TheKthLastNodeVal {
static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;
int TheKthLastNodeVal(int k) {
if (head == null) return -1;
if (k <= 0) return -1;
ListNode fast = head;
ListNode slow = head;
int count = 0;
while (k-1 != count) {
fast = fast.next;
if (fast == null) {
return -1;
}
count++;
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
return slow.val;
}
}思路:
具体如下:
public class Partition {
static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;
public ListNode partition (int x) {
ListNode cur = head;
ListNode ff = null;
ListNode fl = null;
ListNode lf = null;
ListNode ll = null;
while (cur != null) {
if (cur.val < x) {
if (ff == null) {
ff = fl = cur;
} else {
fl.next = cur;
fl = fl.next;
}
} else {
if (lf == null) {
lf = ll = cur;
} else {
ll.next = cur;
ll = ll.next;
}
}
cur = cur.next;
}
if (ff == null) { //第一个子链表为空
return lf;
}
// if (lf == null) { //第二个子链表为空,没必要写
// return ff;
// }
fl.next = lf; //把两个子链表拼接起来
if (lf != null) { //强制把尾节点的next置空
ll.next = null;
}
return ff;
}
}思路:
具体如下:
public class MergeTwoList {
static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;
}
public class Test {
MergeTwoList.ListNode mergeTwoList(MergeTwoList.ListNode headA,
MergeTwoList.ListNode headB) {
MergeTwoList.ListNode newHead = new MergeTwoList.ListNode(-1);
MergeTwoList.ListNode temp = newHead;
while (headA!=null && headB!=null) {
if (headA.val < headB.val) {
temp.next = headA;
temp = headA;
headA = headA.next;
} else {
temp.next = headB;
temp = headB;
headB = headB.next;
}
}
if (headA != null) {
temp.next = headA;
}
if (headB != null) {
temp.next = headB;
}
return newHead.next;
}
}思路:
具体如下:
public class GetIntersectionNode {
static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;
}
public class Test {
public GetIntersectionNode.ListNode getIntersectionNode(GetIntersectionNode.ListNode headA,
GetIntersectionNode.ListNode headB) {
GetIntersectionNode.ListNode pl = headA; //指向长链表的指针
GetIntersectionNode.ListNode ps = headB; //指向短链表的指针
int lenA = 0; //长链表的长度
int lenB = 0; //短链表的长度
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;
len = lenB - lenA;
}
while (len != 0) { //指向长链表的指针先走len步
pl = pl.next;
len--;
}
while (pl != ps) { //两指针一起走
pl = pl.next;
ps = ps.next;
}
if (pl == null) return null; //当长指针为空时,短指针逻辑上来说应比长指针走得快,此时已经为空,说明两链表没有交点
return pl;
}
}思路:
具体实现如下:
public class HasCycle {
static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;
public boolean hasCycle() {
ListNode fast = head;
ListNode slow = head;
while (fast!=null && fast.next!=null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) return true;
}
return false;
}
}思路:
原理:

具体实现如下:
public class DetectCycle {
static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;
public ListNode detectCycle() {
// 判断是否有环
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 null; // 该链表没有环
// 寻找入环点
slow = head;
while (fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
}通过以上几道经典的链表面试题,相信读者对链表的原理结构已经十分了解了~
完