题目:实现一个算法,假设只知道头节点不知道链表长度,找出单向链表中倒数第k个结点
思路:用两个游标p1,p2,p1放在头节点,p2放在头节点+k个元素的位置。然后两个游标一同向尾部移动,直到p2变成尾节点,可以得出p1所在的元素。
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
//哑元
ListIntNode head = new ListIntNode(0);
ListIntNode pre = head;
for (int i = 0; i < arr.length; i++) {
pre.next = new ListIntNode(arr[i]);
pre = pre.next;
}
System.out.println("arr="+head);
while (true){
Scanner scanner = new Scanner(System.in);
int k = scanner.nextInt();
findKthNode(head,k);
}
}
public static class ListIntNode{
int value;
ListIntNode next;
public ListIntNode(int value) {
this.value = value;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder("[");
ListIntNode nextNode = this;
// 跳过哑元节点
if (this.value == 0 && this.next != null) {
nextNode = this.next;
}
while (nextNode != null){
//拼接下一个节点的值,哑元值不打印
sb.append(nextNode.value);
sb.append(",");
nextNode = nextNode.next;
}
if (sb.length() > 1) {
// 移除最后一个逗号
sb.setLength(sb.length() - 1);
}
sb.append("]");
return sb.toString();
}
}
//打印倒数k节点
private static void findKthNode(ListIntNode node, int k) {
if (node == null || k<=0){
System.out.println("node不能为空或k不能小于等于0!");
return;
}
ListIntNode p1 = node;
ListIntNode p2 = node;
ListIntNode p2NextNode = p2.next;
int index = 1;
while (index <= k && p2NextNode != null){
index ++;
p2NextNode = p2NextNode.next;
}
if (k >= index){
System.out.println("k长度不能大于node的长度!");
return;
}
ListIntNode p1NextNode = p1.next;
while (p2NextNode != null){
p1NextNode = p1NextNode.next;
p2NextNode = p2NextNode.next;
}
System.out.println(p1NextNode.value);
}
运行结果如图
题目:用基准值将链表分区
1.编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
2.给定一个链表的头指针 ListNode*pHead,请返回重新排列后的链表的头指针。
3.注意:分割以后保持原来的数据顺序不变。
4.不要开辟新的空间,即不要新建节点
思路:
- 把原始链表根据基准值分区,分为左右两个链表,链表只关注头尾节点。小于基准值的放在左链表,大于等于基准值的放在右链表。
- 这里要注意遍历链表如果不开辟新空间,则需要先保存next节点,把原链表的next节点设置为null,再把原链表设置为next节点,这样做的目的是防止形成循环链表。
- 注意边界,连接左右链表时,左侧链表可能为空,右侧链表尾节点next需要设置为null。
public static void main(String[] args) {
int[] arr = {11,2,13,14,5,6,7,8,9,10,1,12,3,4,15};
//哑元
ListIntNode head = new ListIntNode(0);
ListIntNode pre = head;
for (int i = 0; i < arr.length; i++) {
pre.next = new ListIntNode(arr[i]);
pre = pre.next;
}
int x = 8;
System.out.println("arr= "+head);
System.out.println("链表分区后为: "+partition(head,x));
}
private static ListIntNode partition(ListIntNode head, int x) {
ListIntNode p = head.next;
ListIntNode leftHead = null;
ListIntNode leftTail = null;
ListIntNode rightHead = null;
ListIntNode rightTail = null;
while (p != null){
int value = p.value;
// 保存下一个节点,避免断链
ListIntNode next = p.next;
if (value < x){
if (leftTail == null){
leftHead = p;
leftTail = p;
}
leftTail.next = p;
leftTail = leftTail.next;
}else {
if (rightTail == null){
rightHead = p;
rightTail = p;
}
rightTail.next = p;
rightTail = rightTail.next;
}
// 断开原链表连接防止环形链表
p.next = null;
p = next;
}
//左侧链表为空直接返回右侧链表
if (leftTail == null){
return rightHead;
}
// 连接左右两部分
System.out.println("左:"+leftHead);
System.out.println("右:"+rightHead);
leftTail.next = rightHead;
if (rightHead != null){
// 确保右侧链表尾部指向 null
rightTail.next = null;
}
return leftHead;
}
public static class ListIntNode{
int value;
ListIntNode next;
public ListIntNode(int value) {
this.value = value;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder("[");
ListIntNode nextNode = this;
// 跳过哑元节点
if (this.value == 0 && this.next != null) {
nextNode = this.next;
}
while (nextNode != null){
//拼接下一个节点的值,哑元值不打印
sb.append(nextNode.value);
sb.append(",");
nextNode = nextNode.next;
}
if (sb.length() > 1) {
// 移除最后一个逗号
sb.setLength(sb.length() - 1);
}
sb.append("]");
return sb.toString();
}
}
运行结果如图