206. Reverse Linked List
解法:建立節點
class Solution {
public ListNode reverseList(ListNode head) {
ListNode p=new ListNode(0);
p.next=null;
while(head!=null){
ListNode temp=head;
head=head.next;
temp.next=p.next;
p.next=temp;
}
return p.next;
}
}
92. Reverse Linked List II
解法:建立節點+記錄首尾節點
class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode p0=new ListNode(0);
p0.next=head;
ListNode p=p0;
int count=1;
while(count<m){
p=p.next;
count++;
}
count=n-m+1;
ListNode q=new ListNode(0);
ListNode start=p,tail=p.next,t=p.next;
q.next=null;
while(count>0){
count--;
ListNode temp=t;
t=t.next;
temp.next=q.next;
q.next=temp;
}
start.next=q.next;
tail.next=t;
return p0.next;
}
}
Remove Nth Node From End of List
解法:快慢指針+字首節點(為了處理删除的是頭節點)
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode p=new ListNode(0);
p.next=head;
ListNode q=p;
while(n>0){
q=q.next;
n--;
}
ListNode low=p;
while(q.next!=null){
q=q.next;
low=low.next;
}
ListNode temp=low.next;
low.next=temp.next;
temp.next=null;
return p.next;
}
}
92. Reverse Linked List II
解法:大問題分解成小問題;首先将連結清單分成前後兩部分,然後對後半部分進行反轉操作,然後合并。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void reorderList(ListNode head) {
if(head==null || head.next==null)
return ;
ListNode t1=head;
int len=0;
while(t1!=null){
t1=t1.next;
len++;
}
int mid=(len+1)/2;
ListNode p0=new ListNode(0);
t1=head;
int count=1;
while(count<mid){
count++;
t1=t1.next;
}
ListNode p1=head,p=p0;
ListNode q1=t1.next;
t1.next=null;
ListNode q2=new ListNode(0);
q2.next=null;
while(q1!=null){
ListNode temp=q1;
q1=q1.next;
temp.next=q2.next;
q2.next=temp;
}
q1=q2.next;
while(p1!=null && q1!=null){
ListNode temp=p1;
p1=p1.next;
temp.next=null;
p.next=temp;
p=p.next;
temp=q1;
q1=q1.next;
temp.next=null;
p.next=temp;
p=p.next;
}
if(p1!=null)
p.next=p1;
else if(q1!=null)
p.next=q1;
head=p0.next;
}
}
86. Partition List
解法:雙鍊+合并
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode small=new ListNode(0);
small.next=null;
ListNode big=new ListNode(0);
big.next=null;
ListNode s=small,b=big;
while(head!=null){
ListNode temp=head;
head=head.next;
temp.next=null;
if(temp.val<x){
s.next=temp;
s=s.next;
}else{
b.next=temp;
b=b.next;
}
}
s.next=big.next;
return small.next;
}
}
23. Merge k Sorted Lists
解法:分治+遞歸+合并
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
return merge(lists,0,lists.length-1);
}
ListNode merge(ListNode lists[],int s,int e){
if(e>s){
int mid=(s+e)/2;
ListNode low=merge(lists,s,mid);
ListNode high=merge(lists,mid+1,e);
ListNode p=new ListNode(0);
ListNode p1=p;
p1.next=null;
while(low!=null && high!=null){
if(low.val<high.val){
p1.next=low;
low=low.next;
p1=p1.next;
p1.next=null;
}else{
p1.next=high;
high=high.next;
p1=p1.next;
p1.next=null;
}
}
p1.next=low==null?high:low;
return p.next;
}else if(s==e)
return lists[s];
else
return null;
}
}
25. Reverse Nodes in k-Group
解法:快慢指針+儲存下次計數的首尾+連結清單反轉
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if(k<=1)
return head;
ListNode p=new ListNode(0);
p.next=head;
ListNode fast=p,low=p;
int count=0;
while(fast.next!=null){
fast=fast.next;
count++;
if(count==k){
ListNode newstart=low.next;
ListNode nextend=fast.next;
count=0;
ListNode s=new ListNode(0);
s.next=null;
ListNode t1=low.next;
while(t1!=nextend){
ListNode temp=t1;
t1=t1.next;
temp.next=s.next;
s.next=temp;
}
low.next=s.next;
newstart.next=nextend;
low=newstart;
fast=newstart;
}
}
return p.next;
}
}