面试题
- 1. 删除链表中的节点
- 2. 反转一个链表(非递归解法)
- 3. 判断一个链表是否有环(快慢指针)
- 问题:[快慢指针为什么一定会相遇](https://blog.csdn.net/Leslie5205912/article/details/89386769)
- 4. 获取单链表的节点个数
- 5. 查询单链表中倒数第K个节点
- 6. 逆序打印单向链表(栈)
- 7. 约瑟夫环问题
- 1. 构建环形链表并遍历
- 2. 出圈
1. 删除链表中的节点
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
}
2. 反转一个链表(非递归解法)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode newHead = null;
while (head != null) {
ListNode tmp = head.next;
head.next = newHead;
newHead = head;
head = tmp;
}
return newHead;
}
}
3. 判断一个链表是否有环(快慢指针)
/**
* 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 slow = head;
ListNode fast = head.next; // 快指针每次都比慢指针快一步(包括开始)
while (fast != null && fast.next != null) {
if (slow.val == fast.val) return true;
slow = slow.next;
fast = fast.next.next;
}
return false;
}
}
问题:快慢指针为什么一定会相遇
慢指针每次移动一格,快指针每次移动两格,在有环的链表里,他们一定会相遇
1、当快指针就在慢指针后面,那么下一次慢指针移动一位,快指针移动两位,相遇
2、当快指针和慢指针差一个位置,那么下一次慢指针移动一位,快指针移动两位,他们会变成第一种情况
3、当快指针和慢指针差两个位置,那么下一次慢指针移动一位,快指针移动两位,他们会变成第二种情况
我知道你也在纠结为什么会没有快指针跳过慢指针他们没有相遇的这种情况发生,但是这种情况只会发生在他们相遇后的下一次移动
原因:其实从上面的三步不难看出:快指针是一格一格追赶慢指针的,即他们的距离是…4->3->2->1->0这样缩短的。所以一定会相遇
4. 获取单链表的节点个数
public static int getLinkListNodeLength(Node head){
if(head == null){
return 0;
}
int length = 0;
Node currentNode = head;
//如果链表不为空,则长度加1并且当前结点指向下一个结点
while(currentNode != null){
length++;
currentNode = currentNode.next;
}
return length;
}
5. 查询单链表中倒数第K个节点
static Node getLastIndex(Node first, int k) {
//1. 链表为空
if (first == null) {
return null;
}
//2.遍历得到链表长度
int length = getLinkListNodeLength(first);
//2.1 k校验
if (k < 0 || k > length) {
return null;
}
//3. 从第一个开始遍历,遍历到length-k结束
Node cur = first;
for (int i = 0; i < length - k; i++) {
cur = cur.next;
}
return cur;
}
6. 逆序打印单向链表(栈)
LinkedList linkedList = new LinkedList<Integer>();
linkedList.add(0,0);
linkedList.add(1,1);
linkedList.add(2,2);
linkedList.add(3,3);
Stack<Integer> stacks = new Stack<>(); //先进后出
//将元素压入栈
LinkedList.Node cur = linkedList.getFirst();
while (cur != null) {
stacks.push((Integer) cur.element);
cur = cur.next;
}
//元素出栈
while (stacks.size() > 0) {
System.out.println(stacks.pop());
}
7. 约瑟夫环问题
1. 构建环形链表并遍历
2. 出圈
public class JosepfuDemo {
public static void main(String[] args) {
CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
circleSingleLinkedList.addBoy(5);
circleSingleLinkedList.show();
circleSingleLinkedList.countBoy(1, 2, 5);
}
}
class CircleSingleLinkedList {
private Boy first;
void addBoy(int nums) {
Boy cur = null;//指向最后一个节点
for (int i = 1; i <= nums; i++) {
Boy boy = new Boy(i);
if (i == 1) {
first = boy;
first.setNext(first); //组成环
cur = boy;
} else {
cur.setNext(boy); //当前节点加入环
boy.setNext(first); //组成环
cur = boy; //指向最后一个节点
}
}
}
/**
* @param startNo 从第几个小孩开始
* @param countNum 数多少下
* @param nums 一共多少小孩
*/
void countBoy(int startNo, int countNum, int nums) {
// 先对数据进行校验
if (first == null || startNo < 1 || startNo > nums) {
System.out.println("参数输入有误, 请重新输入");
return;
}
//创建辅助指针,指向最后一个元素
Boy helper = first;
while (true) {
if (helper.getNext() == first) {
break;
}
helper = helper.getNext();
}
//小孩报数前,先让 first 和 helper 移动 k - 1次
for (int i = 0; i < startNo - 1; i++) {
helper = helper.getNext();
first = first.getNext();
}
while (true) {
if (first == helper) {
System.out.println("最后一个小孩:" + first.getNo());
break;
}
//让 first 和 helper 移动 m - 1次 (假设m是2,只需要移动一次,自己喊1,下一个就是被移除的元素)
for (int i = 0; i < countNum - 1; i++) {
helper = helper.getNext();
first = first.getNext();
}
//这时first指向的节点,就是要出圈的小孩节点
System.out.println("出圈:" + first.getNo());
//元素出圈
first = first.getNext();
helper.setNext(first);
}
}
void show() {
Boy cur = first;
while (true) {
System.out.println(cur.getNo());
if (cur.getNext() == first) {
break;
}
cur = cur.getNext();
}
}
}
class Boy {
private int no;
private Boy next;
public Boy(int no) {
this.no = no;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public Boy getNext() {
return next;
}
public void setNext(Boy next) {
this.next = next;
}
}
问题1-3参考:https://luzhenyu.blog.csdn.net/article/details/104609375
问题4参考:获取单链表的节点个数:https://blog.csdn.net/jcm666666/article/details/52278471
快慢指针为什么一定会相遇:https://blog.csdn.net/Leslie5205912/article/details/89386769
约瑟夫环问题:https://www.bilibili.com/video/BV1E4411H73v?p=28&vd_source=b901ef0e9ed712b24882863596eab0ca