牛客网---部分链表题(JAVA)
链表中的节点每k个一组反转。
描述:将给定的链表中的节点每个k一组反转,返回反转后的链表,如果链表中的节点数不是k的倍数,将最后剩下的节点保持原样。
输入12345,k=3
输出32145
思路(正常思路):
- 获得该链表的长度,对前len/k个组进行反转操作。
- 如果最后有剩余,连接到该链表上。
注意:对于完整的组进行反转操作时,反转前的head为反转后的tail。且要将其与上一节的tail相连,因此需要保存上一节的tail。
代码参考:
public static ListNode reverseKGroup (ListNode head, int k) {
ListNode result=new ListNode(0);
ListNode next=null;
ListNode tail_pre=result;
ListNode tail=null;
int i;
int len=getLen(head);
ListNode now=result;
for(int j=0;j<len/k;j++){
tail=head;
ListNode tmp=null;
for(i=0;i<k;i++){
next=head.next;
head.next=tmp;
tmp=head;
head=next;
}
now.next=tmp;
now=tail;
}
if (head!=null) {
now.next=head;
}
return result.next;
}
private static int getLen(ListNode head) {//获得链表长度
// TODO Auto-generated method stub
int len=0;
while(head!=null) {
len++;
head=head.next;
}
return len;
}
链表求和
描述:假设链表中每一个节点的值都在 0 - 9 之间,那么链表整体就可以代表一个整数。给定两个这种链表,请生成代表两个整数相加值的结果链表。
数据范围:0≤n,m≤10000000≤n,m≤1000000,链表任意值 0≤val≤90≤val≤9
要求:空间复杂度 O(n)O(n),时间复杂度 O(n)O(n)
例如:链表 1 为 9->3->7,链表 2 为 6->3,最后生成新的结果链表为 1->0->0->0。
思路:利用栈的先进后出进行数据计算。
代码参考:
public Stack<Integer> listtoint(ListNode head){//将链表中的数存入栈中
Stack<Integer> res=new Stack<>();
while(head!=null){
res.push(head.val);
head=head.next;
}
return res;
}
public ListNode addInList (ListNode head1, ListNode head2) {
// write code here
Stack<Integer> a=listtoint(head1);
Stack<Integer> b=listtoint(head2);
int num1=a.pop(),num2=b.pop(),sum=num1+num2;
int jw=sum/10;
ListNode Res=new ListNode(sum%10);
while(!a.empty()||!b.empty()||jw!=0){//将计算结果存入链表中
if(a.size()==0)
num1=0;
else
num1=a.pop();
if(b.size()==0)
num2=0;
else
num2=b.pop();
sum=num1+num2+jw;
ListNode LN=new ListNode(0);
LN.val=sum%10;
LN.next=Res;
Res=LN;
jw=sum/10;
}
return Res;
}
判断一个链表是否为回文结构
描述:给定一个链表,请判断该链表是否为回文结构。回文是指该字符串正序逆序完全一致。
思路:使用快慢指针寻找到链表的中点,同时利用栈,比较前后段的链表是否一致。
注意:处理单数或双数大小的链表时的中间位置。
public boolean isPail (ListNode head) {
// write code here
if(head==null||head.next==null)
return true;
ListNode slow=new ListNode(-1);
ListNode fast=new ListNode(-1);
slow.next=head;//这样双数大小时,前后段链表大小一样。单数大小时,需要处理最中间的位置。
fast.next=head;
Stack<Integer> stack=new Stack<>();//import java.util.*;
while(fast.next!=null){
fast=fast.next;
slow=slow.next;
if(fast.next!=null){
fast=fast.next;
stack.push(slow.val);
}
}
while(slow.next!=null&&!stack.empty()){
slow=slow.next;
if(stack.peek()!=slow.val)
return false;
stack.pop();
}
return true;
}
删除有序链表中重复出现的元素
描述:给出一个升序排列的链表,删除链表中所有重复出现的元素,只保留原链表中只出现一次的元素。
例如:1→2→3→3→4→4→5, 返回1→2→5.
思路:使用flag模式,对每个数值的元素进行判断,如果重复出现则将flag置为1,否则置为0。对于flag=0的元素加入新链表中,对flag=1的元素直接抛弃,判断下一个数值的元素。
注意:链表尾部的处理,比如1→2→3→3→4→5→5。
代码参考:
public ListNode deleteDuplicates (ListNode head) {
// write code here
if(head==null||head.next==null)
return head;
ListNode newhead=new ListNode(0);
ListNode scan=head,p1=newhead;
int flag=0;
while(scan!=null){
while(scan.next!=null&&scan.val==scan.next.val){
scan=scan.next;
flag=1;
}
if(flag==0){
p1.next=scan;
p1=scan;
}
scan=scan.next;
if(scan==null){//这里包含对最后一个元素重复时的处理
p1.next=scan;
break;
}
flag=0;
}
return newhead.next;
}
本文作者:梅落南山
本文链接:https://www.cnblogs.com/ting65536/p/17197790.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步