After learning the singly linked list, I did a few interview questions while the iron was hot. (Click on the link to view the previous singly linked list study )
The difficulty of the following 1-5 questions increases in order, and the idea and implementation process of each question will be displayed in detail.
- Find the number of nodes in a singly linked list
- Find the kth node from the bottom in the singly linked list [Sina Interview Question]
- Reversal of singly linked list [Tencent Interview Questions]
- Print singly linked list from end to beginning [Baidu, request method 1: reverse traversal, method 2: Stack stack]
- Merging two ordered singly linked lists, the linked list is still in order after the merge
Before completing each question, you can use your own understanding to complete it. Below I will only talk about my thinking analysis and the implementation process of the code.
01 Find the number of nodes in a singly linked list
Thinking analysis:
1. Determine whether it is empty
2. Loop traversal-if the next node of the auxiliary node is not equal to null, then length++
specific code:
/**
* @param head 头节点
* @return 返回的是单链表有效节点的个数
*/
public static int findSingleLinkLength(HeroNode head) {
//辅助节点
HeroNode cur = head.next;
if (cur == null) {
return 0;
}
//定义一个变量接收节点的个数
int length = 0;
while (cur != null) {
//后移 接着遍历寻找
cur = cur.next;
length++;
}
return length;
}
Code test:
public class SingleLinkedListDemo {
public static void main(String[] args) {
SingleLinkedList singleLinkedList = new SingleLinkedList();
HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");
HeroNode hero5 = new HeroNode(5, "李逵", "黑旋风");
//添加英雄
singleLinkedList.add(hero1);
singleLinkedList.add(hero3);
singleLinkedList.add(hero5);
singleLinkedList.add(hero2);
//测试单链表有效节点的个数
int length = findSingleLinkLength(singleLinkedList.getHead());
System.out.println("length=" + length);
}
}
Test Results:
length=4
02Find the kth node from the bottom in the singly linked list [Sina Interview Question]
Analysis of ideas:
1. Loop through (total number of nodes-number of target nodes).
For example: a total of 4 nodes. Now we need to find the third node from the bottom. The way to do this is to find this node through the loop (4-3), where 4 is passed Call the method findSingleLinkLength to get, 3 is the value passed in by the user.
Specific code:
//查找单链表中倒数第K个节点【新浪面试】
public static HeroNode findHeroNode(HeroNode head, int no) {
//定义辅助节点指向第一个节点
HeroNode cur = head.next;
//找出总的节点数
int size = findSingleLinkLength(head);
//对传入的no进行校验
//不存在倒数第0个节点的说法 也不存在要查找的节点数大于总节点数
if (no <= 0 || no > size) {
return null;
}
//循环遍历 假如 3个节点 查找倒数第二个节点 3-2=1
for (int i = 0; i < size - no; i++) {
//后移
cur = cur.next;
}
return cur;
}
Code test:
public class SingleLinkedListDemo {
public static void main(String[] args) {
SingleLinkedList singleLinkedList = new SingleLinkedList();
HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");
HeroNode hero5 = new HeroNode(5, "李逵", "黑旋风");
//添加英雄
singleLinkedList.add(hero1);
singleLinkedList.add(hero3);
singleLinkedList.add(hero5);
singleLinkedList.add(hero2);
//测试单链表中倒数第K个节点
int no = 3;
HeroNode heroNode = findHeroNode(singleLinkedList.getHead(), no);
System.out.printf("倒数第%d节点", no);
System.out.println(heroNode);
}
}
Test Results:
The third-to-last node HeroNode{no=3, name='Wu Yong', nickname='Zhiduoxing'}
03 Reversal of singly linked list [Tencent interview questions]
Analysis of the train of thought:
1. First define a node HeroNode reverse = new HeroNode(0,"","");
2. Traverse the original linked list from beginning to end, take it out every time a node is traversed, and place it in the new linked list The forefront of reverseHead
3. The head.next of the original linked list = reverseHead.next
//单链表的反转
public static void reverseSingleLink(HeroNode head) {
//没有节点或者只存在一个节点
if (head.next == null || head.next.next == null) {
return;
}
//创建一个新的单链表
HeroNode reverseHead = new HeroNode(0,"","");
//定义一个辅助变量,帮助我们遍历原来的链表
HeroNode cur = head.next;
HeroNode next = null;//指向当前节点的下一个节点
while (cur != null) {
//保留当前节点的下一个节点
next = cur.next;
//cur的下一个节点指向新链表的前端
cur.next = reverseHead.next;
//将cur连接到新的链表上
reverseHead.next = cur;
cur = next;//节点后移
}
//将 head.next 指向 reverseHead.next 实现单链表的反转
head.next = reverseHead.next;
}
Code test:
public class SingleLinkedListDemo {
public static void main(String[] args) {
SingleLinkedList singleLinkedList = new SingleLinkedList();
HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");
HeroNode hero5 = new HeroNode(5, "李逵", "黑旋风");
//添加英雄
singleLinkedList.add(hero1);
singleLinkedList.add(hero3);
singleLinkedList.add(hero5);
singleLinkedList.add(hero2);
System.out.println("反转前的链表");
singleLinkedList.list();
System.out.println("测试反转后的链表");
reverseSingleLink(singleLinkedList.getHead());
singleLinkedList.list();
}
}
Test Results:
The linked list before the reversal
HeroNode{no=1, name='Songjiang', nickname='Timely Rain'}
HeroNode{no=3, name='Wu Yong', nickname='Zhiduoxing'}
HeroNode{no=5, name= '
李逵', nickname='Black Whirlwind'} HeroNode(no=2, name='Lu Junyi', nickname='Jade Qilin'}
Test the reversed linked list
HeroNode(no=2, name='Lu Junyi', nickname= 'Jade Qilin'}
HeroNode{no=5, name='Li Kui', nickname='Black Whirlwind'}
HeroNode{no=3, name='Wu Yong', nickname='智多星'}
HeroNode{no=1, name ='Song Jiang', nickname='Timely rain'}
04 Print singly linked list from end to beginning [Baidu, request method 1: reverse traversal, method 2: Stack stack]
Thinking analysis:
1. Use the first-in-last-out feature of the stack to achieve reverse printing.
The basic usage of the stack: stacking, popping
Stack<String> stack = new Stack<>();
//入栈
stack.push("张三");
stack.push("王五");
stack.push("赵六");
//出栈
while (stack.size() > 0){
System.out.println(stack.pop());
}
Print result:
Zhao Liu
Wang Wu
Zhang San
To achieve reverse printing of the stack:
//实现链表的逆序打印
public static void reversePrint(HeroNode head){
//判断链表是否为空
if (head.next == null){
return;
}
//创建栈,将各个节点压入栈中
Stack<HeroNode> stack = new Stack<>();
//辅助变量指向节点
HeroNode cur = head.next;
//入栈
while (cur != null) {
stack.push(cur);
//节点后移 下一个节点压入栈
cur = cur.next;
}
//出栈
while (stack.size() > 0){
System.out.println(stack.pop());
}
}
Code test:
public class SingleLinkedListDemo {
public static void main(String[] args) {
SingleLinkedList singleLinkedList = new SingleLinkedList();
HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");
HeroNode hero5 = new HeroNode(5, "李逵", "黑旋风");
//添加英雄
singleLinkedList.add(hero1);
singleLinkedList.add(hero3);
singleLinkedList.add(hero5);
singleLinkedList.add(hero2);
//循环遍历
System.out.println("单链表的信息");
singleLinkedList.list();
System.out.println("测试逆序打印链表");
reversePrint(singleLinkedList.getHead());
}
}
Test Results:
The information of the
singly linked list HeroNode{no=1, name='Song Jiang', nickname='Timely Rain'}
HeroNode{no=3, name='Wu Yong', nickname='智多星'}
HeroNode{no=5, name=' Li Kui', nickname='Black Whirlwind'}
HeroNode(no=2, name='Lu Junyi', nickname='玉麒麟'}
Test reverse printing of the linked list
HeroNode{no=2, name='Lu Junyi', nickname='玉kirin '}
HeroNode{no=5, name='Li Kui', nickname='Black Whirlwind'}
HeroNode{no=3, name='Wu Yong', nickname='Zhiduoxing'}
HeroNode{no=1, name='Song Jiang ', nickname='Timely rain'}
The above are some interview questions for singly linked lists. The method is not the only way to try multiple methods.