当前位置:网站首页>Flip linked list
Flip linked list
2022-07-19 06:41:00 【hello,world_ yzs】
Single list flip
recursive
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null)
return head;
ListNode rever = reverseList(head.next);
//1->2->3->4, here root=3,re=root.next=4,1->2->3,4->3
head.next.next = head;
head.next = null;
return rever;
}
Non recursive
public static Node Traversing(Node root) {
if(root ==null)
return null;
Node preNode = null;
Node curNode = root;
Node nextNode =null;
while(curNode!=null) {
nextNode = curNode.next;
curNode.next =preNode;
preNode =curNode;
curNode = nextNode;
}
return preNode;
}
Two or two exchange nodes in a linked list
public ListNode swapPairs(ListNode head) {
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode q = dummyHead;
while(q.next != null && q.next.next != null) {
ListNode node1 = q.next;
ListNode node2 = node1.next;
ListNode nnext = node2.next;
node2.next = node1;
node1.next = nnext;
q.next = node2;
q = node1;
}
return dummyHead.next;
}

K A set of flip list
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if(head == null)
return null;
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode pre = dummyHead;
ListNode end = dummyHead;
while(end.next != null) {
for(int i = 0; i < k && end != null; i++) {
end = end.next;
}
if(end == null)
break;
// It is necessary to record the precursor and successor of the flipped linked list , It is convenient to connect the flipped part and the non flipped part after flipping
// Two variables are required initially pre and end,pre Represents the precursor of the linked list to be flipped ,end Represents the end of the linked list to be flipped
// after k This cycle ,end To the end , Record the successor of the linked list to be flipped next = end.next
// Flip list , Then connect the three parts of the linked list , Then reset pre and end The pointer , Then go to the next cycle
ListNode start = pre.next;
ListNode pnext = end.next;
end.next = null;//reverse:start~end,end Back broken
pre.next = reverse(start);//pre:join The first part is the precursor and the overturned linked list
start.next = pnext;//end~start:join Flipped and not flipped
pre = start;
end = pre;
}
return dummyHead.next;
}
private ListNode reverse(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while(cur != null) {
ListNode pnext = cur.next;
cur.next = pre;
pre = cur;
cur = pnext;
}
return pre;//not return cur:cur =null this time.
}
}



边栏推荐
- Restapi implements aggregation (dark horse tutorial)
- 《PyTorch深度学习实践》-B站 刘二大人-day3
- 你见过的最差的程序员是怎样的?
- Busybox specified date modification temporarily does not require clock -w to write to hardware
- Vscode one dark and C extended variable color conflict settings JSON is as follows
- UDP的报文结构
- Design and implementation of a gesture control system for tablet computer based on gaze
- 斑点检测 记录
- 吴恩达机器学习第8-9章
- 颜色直方图 灰度图&彩色图
猜你喜欢

人脸识别错误

Handle Chinese word segmentation IK word segmenter and expand and stop dictionary

海量数据

Solution: unable to load file c:\program files\ Because running scripts is forbidden on this system

Interview review nth time

视图、索引文件的应用

Automatic completion & (custom) Pinyin word Separator &

Restclient query document

Introduction to daily use of manjaro system

Manjaro 系统日常使用入门导引
随机推荐
Learning non posture gaze deviation with head movement
Attention prediction in self video based on motion and visual prominence
Manjaro 系统日常使用入门导引
吴恩达机器学习第1-2章
实验五: GUI
Résoudre le problème de l'ambiguïté de la cible dans l'interaction de fixation 3D par l'estimation de la profondeur vor
Interview review nth time
Perception de l’état d’attention des utilisateurs sur les smartphones
sql的约束条件
工作中遇到的一些问题
What kind of deep learning is most suitable for your enterprise?
翻转链表
redis
Restclient query document
吴恩达机器学习第12-13章
实验四 运算符重载和虚函数
无80和443端口下申请域名SSL证书(适用于 acme.sh 和 certbot)
Using VOR depth estimation to solve the problem of target ambiguity in three-dimensional gaze interaction
UDP message structure
《PyTorch深度学习实践》-B站 刘二大人-day6