reverseLinkedList(翻转链表)
ReverseLinkedList(翻转链表)
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。非连续、非顺序指的是,通过指针把一组零散的内存块串联在一起,其中每一个内存块叫做链表的节点,所以每个节点包含两部分一个data(你存放的数据),一个next(指向下一个节点)当然这里使用单向链表举例。双向链表则有两个指向节点一个next(下一个节点)一个prev(上一个节点),对比:双向链表虽然更麻烦,但是比单向链表更受欢迎,因为,他记录了上一个节点,节省了操作数据时候的时间,但是需要用内存换时间。
单向链表:
双向链表;
题目一:
给定一组数据,从尾到头进行翻转
Input: 1 ->2->3->4->5
Output: 5->5->3->2->1
思路:使用是三个变量进行控制(prev、current、next),模拟一个窗口,进行对数据的推进,进而翻转,不断改变三个元素的位置。
/**
* @author : lizi
* @date : 2021-03-02 17:05
**/
public class ListNode {
int val = 0;
ListNode next; ListNode(int x) {
val = x;
} public void setVal(int val) {
this.val = val;
} public void setNext(ListNode next) {
this.next = next;
}
}
private static ListNode reverseList(ListNode head) {
if (head == null) return null;
ListNode prev=head;
ListNode current=head.next;
// because the last Node point into null,so let fist node.next point into null
prev.next=null;
while (current!=null){
ListNode next=current.next;
// that is place where change node. change position of current and prev
current.next=prev;
prev=current;
// change current into next (in the way,the window is pushed)
current=next;
}
return prev;
}
}
题目二:
obviously,we should change position of m and n
Input:1->2->3->4->5->null ,m=2,n=5
Output:1->4>3>2>5>null
tip: actually, many companies like to make this question as ultimate question,also the question was Amazon’s question for interviewee,however, we remain can use method in reversing whole linkedList. i mean that method of pushing window.
private static ListNode reverseBetween(ListNode head, int m, int n) {
// if you can not find head, you can make the node.next to find head, todo actually, it is a node that prevent you can not find first node
ListNode dummy = new ListNode(-1);
dummy.next=head;
head=dummy;
// to find a place where you began to control list
for (int i = 1; i <m ; i++) {
head=head.next;
}
ListNode prevM=head;
ListNode mNode=prevM.next;
ListNode nNode=mNode;
ListNode postN=nNode.next;
//todo visibly, the portion is method of window that i mention in method of reversing the whole window
for (int i = m; i <n ; i++) {
ListNode next=postN.next;
postN.next=nNode;
nNode=postN;
postN=next;
}
prevM.next=nNode;
mNode.next=postN;
// you can get dummy node.next in this way ,find the first node
return dummy.next;
}
finally: to do examine
public static void main(String[] args) {
ListNode listNode1 = generateDate();
System.out.println(listNode1.toString());
// reverse whole list
ListNode reverseBetween = reverseList(generateDate());
// reverse m and n in list
ListNode listNode = reverseBetween(listNode1, 2, 4);
printForSomething(reverseBetween);
} // just to print result
public static void printForSomething(ListNode listNode) {
if (listNode != null) {
System.out.println(listNode.val);
printForSomething(listNode.next);
}
}
the picture is what i visualize method how to run:
To summarize:
as a beginner,personally, it is abstract to imagine how the cell of linkedlist connect with each other,but it is a effective to draw what you imagine. i still want everyone to criticize what i wrong in every article.
最新文章
- AngularJS的基础元素应用
- 转载:移动web开发规范
- java基础-servlet-2:生命周期
- 直播CDN架构随想
- POJ 2697 A Board Game(Trie判重+BFS)
- IOS网络开发(一)
- HDU 3974 Assign the task 暴力/线段树
- C语言嵌入式系统编程修炼之三:内存操作
- ActiveRecord 模式杂谈
- 设计模式(六)桥连模式Bridge(结构型)
- vue-cli中如何引入jquery
- 【转】Apache服务器安全配置
- socket和webService的区别
- linux 启动流程
- Linux背背背(6)
- Codeforces Round #432 Div. 1
- BZOJ4555 HEOI2016/TJOI2016求和(NTT+斯特林数)
- python 全栈开发,Day135(爬虫系列之第2章-BS和Xpath模块)
- C/C++基础----拷贝控制
- Win7 IIS 部署站点遇到的问题 如 HTTP 错误 404.XX
热门文章
- HDU 4649 Professor Tian(概率DP)题解
- 手把手搭建一套私有 npm 服务
- 图解 HTTP, 图解 HTTPS, 图解 HTTP/2, 图解 HTTP/3, 图解 QUIC
- Node.js &; BFF &; FaaS
- Open API collection
- CSS 阴影效果
- vue最好的ssr服务器渲染框架
- django学习-3.如何编写一个html页面并展示到浏览器,及相关导入错误的解决方案
- HTML页面顶部出现空白部分(#65279字符?)解决办法
- 详解Go语言调度循环源码实现