目录
- 数组、链表(Array、Linked List)
- 数组
- 链表
- 面试题
- [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)
- [24. Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)
- [141. Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)
- [142. Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/submissions/)
- [25. Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/)
数组、链表(Array、Linked List)
数组
结构
内存里一段连续的存储区域
查取插入删除
为了改善插入删除,提出了
链表
适用于
- 插入删除操作很多
- 不知道有多少元素
单链表
变形,多了头指针和尾指针
插入
删除
时间复杂度
查找O(n)
插入删除O(1)
双链表
面试题
206. Reverse Linked List
- Reverse Linked List
Easy
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
自己的解题方法一:迭代
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null){return null;}
ListNode next,tmp,curr;
curr=head;
next = head.next;
head.next=null;
while(next!=null){
tmp=next.next;
next.next=curr;
curr=next;
next=tmp;
}
return curr;
}
}
结果:
自己的解题方法二:递归
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null){return null;}
ListNode curr, next;
curr = head;
next = head.next;
head.next=null;
return reverse(curr,next);
}
public ListNode reverse(ListNode curr, ListNode next){
ListNode tmp;
if(next!=null){
tmp=next.next;
next.next=curr;
curr=next;
next=tmp;
return reverse(curr,next);
}
else{
return curr;
}
}
}
结果:
老师解法:
转换为Java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode cur,prev,tmp;
cur= head;
prev = null;
while(cur!=null){
tmp=cur.next;
cur.next=prev;
prev=cur;
cur=tmp;
}
return prev;
}
}
结果:
改进点:将边缘案例包含在初始化条件中
24. Swap Nodes in Pairs
注意:JAVA的“=”是传递的地址!参见:
自己的解法:
class Solution {
public ListNode swapPairs(ListNode head) {
while(cur!= null){
if(cur.next==null || cur.next.next==null){
pre.next=cur.next;
cur.next=pre;
return res;
}else{
pre.next=cur.next.next;
tmp=pre;
pre=cur.next; ;
cur.next=tmp;
cur=pre.next;
}
}
return res;
}
}
结果:
老师的解法:
141. Linked List Cycle
自己的解法1:
注意:Map的参数是reference类型,比如int会报错??,修改为integer。
Java中Map的使用
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode cur=head;
Integer pos=0;
Map<ListNode,Integer> map=new HashMap<ListNode, Integer>();
while(cur!=null){
if(map.containsKey(cur)){pos=map.get(cur);return true;}
else{
map.put(cur,pos);
pos=pos+1;
cur=cur.next;}
}
return false;
}
}
结果:
老师的方法:
142. Linked List Cycle II
自己的解法:
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode cur=head;
Integer pos=0;
Map<ListNode,Integer> map=new HashMap<ListNode, Integer>();
while(cur!=null){
if(map.containsKey(cur)){pos=map.get(cur);return cur;}
else{
map.put(cur,pos);
pos=pos+1;
cur=cur.next;}
}
return null;
}
}
结果:
25. Reverse Nodes in k-Group
自己的解法:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode cur,pre,self,a,b,tmp,stop;
cur=head;
self=head;
a=cur;
pre=null;
stop=new ListNode();
for(int i=0;i<k;i++){
if(head!=null){
self=head;
head=head.next;
}else{
self=cur;
}
}
while(cur!=null){
for(int i=0;i<k;i++){
if(cur!=null){
pre=cur;
cur=cur.next;
}else{
return self;
}
}
stop.next=pre;
b=cur;
stop=a;
for(int i=0;i<k;i++){
tmp=a.next;
a.next=b;
b=a;
a=tmp;
}
}
return self;
}
}
结果: