当前位置:网站首页>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.
}
}



边栏推荐
猜你喜欢

Attention prediction in self video based on motion and visual prominence

二分查找及其引申

Depth first search (DFS for short)

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

Design and implementation of a gesture control system for tablet computer based on gaze

数据库的查询(二)

翻转链表

Experiment 5: Gui

Mapping index attribute & operation of creating index

EOG based eye movement detection and gaze estimation for an asynchronous virtual keyboard
随机推荐
基于视觉显著性的外观注视估计
Dual tone sorting of CUDA and large arrays
Es aggregation analysis reports an error: "reason": "text fields are not optimized for operations
网络层及ip学习
C language specifies how many days to display from the date
DSL realizes automatic completion query
sql的约束条件
2022/07/11 group 5 Ding Shuai's study notes day04
CUDA与大数组的双调排序
实验三 继承性和派生类
山西省第二届网络安全技能大赛(企业组)部分赛题WP(四)
Using VOR depth estimation to solve the problem of target ambiguity in three-dimensional gaze interaction
EOG-based eye movement detection and gaze estimation for an asynchronous virtual keyboard基于EOG的异步虚
Design and implementation of a gesture control system for tablet computer based on gaze
[force buckle] design cycle queue
Introduction to daily use of manjaro system
《PyTorch深度学习实践》-B站 刘二大人-day5
Leetcode tree
Experiment 3 inheritance and derived classes
Automatic completion & (custom) Pinyin word Separator &