1、建立链表:
利用构造方法创建节点时,若指针未赋值时,默认为null
package learn;
public class Demo {
public static class Node{
public int value;
public Node next;
public Node rand;
public Node(int value){
this.value=value;
}
public static void main(String[] args) {
Node n=new Node(1);
System.out.println(n.next);
System.out.println(n.rand);
}
}
}
package learn;
public class Test {
public static class Node{
public int value;
public Node next;
public Node(int value){
this.value=value;
}
}
public static Node add(Node head,int value){
Node cur=new Node(value);
if(head==null){
head=cur;
}else{
cur.next=head;
head=cur;
}
return head;
}
public static void printLinkedList(Node head) {
Node n=head;
while (n!=null){
System.out.print(n.value+" ");
n=n.next;
}
}
public static void main(String[] args) {
Node head=null;
head=add(head,1);
head=add(head,2);
head=add(head,3);
head=add(head,2);
head=add(head,1);
printLinkedList(head);
}
}
2、判断是否为回文链表 (1)利用栈:整个倒入栈,然后逐一弹出,和链表值一一比较 需要n个额外空间
package learn;
import java.util.Stack;
public class Test {
public static class Node{
public int value;
public Node next;
public Node(int value){
this.value=value;
}
}
public static Node add(Node head,int value){
Node cur=new Node(value);
if(head==null){
head=cur;
}else{
cur.next=head;
head=cur;
}
return head;
}
public static void printLinkedList(Node head) {
Node n=head;
while (n!=null){
System.out.print(n.value+" ");
n=n.next;
}
}
public static boolean isPalindrome(Node head) {
Stack<Node> s=new Stack<Node>();
Node p=head;
while (p!=null){
s.push(p);
p=p.next;
}
while (head!=null){
if(head.value!=s.pop().value){
return false;
}
head=head.next;
}
return true;
}
public static void main(String[] args) {
Node head=null;
head=add(head,1);
head=add(head,2);
head=add(head,3);
head=add(head,3);
head=add(head,2);
head=add(head,1);
printLinkedList(head);
boolean b=isPalindrome(head);
System.out.println(b);
}
}
(2)栈+快慢指针:需要n/2的额外空间
public static boolean isPalindrome2(Node head) {
Node n1=head;
Node n2=head;
while(n2.next!=null && n2.next.next!=null){
n1=n1.next;
n2=n2.next.next;
};
n2=n1.next;
Stack<Node> s=new Stack<Node>();
while (n2!=null){
s.push(n2);
n2=n2.next;
}
while (!s.isEmpty()){
if(head.value!=s.pop().value){
return false;
}
head=head.next;
}
return true;
}
(3)快慢指针+链表反转(2次):need O(1) extra space
package learn;
import java.util.Stack;
public class Test {
public static class Node{
public int value;
public Node next;
public Node(int value){
this.value=value;
}
}
public static Node add(Node head,int value){
Node cur=new Node(value);
if(head==null){
head=cur;
}else{
cur.next=head;
head=cur;
}
return head;
}
public static void printLinkedList(Node head) {
Node n=head;
while (n!=null){
System.out.print(n.value+" ");
n=n.next;
}
}
public static boolean isPalindrome3(Node head) {
if(head==null || head.next==null)
{
return true;
}
Node n1=head;
Node n2=head;
boolean res=true;
while(n2.next!=null && n2.next.next!=null){
n1=n1.next;
n2=n2.next.next;
};
n2=n1.next;
n1.next=null;
Node n3;
while(n2!=null){
n3=n2.next;
n2.next=n1;
n1=n2;
n2=n3;
}
n3=n1;
n2=head;
while(n1!=null && n2!=null){
if(n1.value!=n2.value){
res=false;
}
n1=n1.next;
n2=n2.next;
}
n1=null;
while (n3!=null){
n2=n3.next;
n3.next=n1;
n1=n3;
n3=n2;
}
printLinkedList(head);
System.out.println();
return res;
}
public static void main(String[] args) {
Node head=null;
head=add(head,1);
head=add(head,2);
head=add(head,3);
head=add(head,2);
head=add(head,1);
printLinkedList(head);
System.out.println();
boolean b=isPalindrome3(head);
System.out.println(b);
}
}
3、将单链表按某值划分为左边小于,中间相等,右边大于的形式 (1)将值倒进数组,做partition操作,然后将数组中的值依次连成链表 (2)
package learn;
import java.util.Stack;
public class Test {
public static class Node{
public int value;
public Node next;
public Node(int value){
this.value=value;
}
}
public static Node addFromBottom(Node head,int value){
Node cur=new Node(value);
Node p=head;
cur.next=null;
if(head==null){
head=cur;
}else{
while (p.next!=null){
p=p.next;
}
p.next=cur;
}
return head;
}
public static void printLinkedList(Node head) {
Node n=head;
while (n!=null){
System.out.print(n.value+" ");
n=n.next;
}
}
public static Node listPartition2(Node head,int pivot) {
Node sh=null;
Node st=null;
Node eh=null;
Node et=null;
Node bh=null;
Node bt=null;
Node next;
while (head!=null){
next=head.next;
head.next=null;
if(head.value<pivot){
if(sh==null){
sh=head;
st=head;
}else {
st.next=head;
st=head;
}
}else if(head.value==pivot){
if(eh==null){
eh=head;
et=head;
}else {
et.next=head;
et=head;
}
}else {
if(bh==null){
bh=head;
bt=head;
}else {
bt.next=head;
bt=head;
}
}
head=next;
}
if(st!=null){
st.next=eh;
et=(et!=null)?et:st;
}
if(et!=null){
et.next=bh;
}
return sh!=null?sh:(eh!=null?eh:bh);
}
public static void main(String[] args) {
Node head=null;
head=addFromBottom(head,4);
head=addFromBottom(head,6);
head=addFromBottom(head,3);
head=addFromBottom(head,5);
head=addFromBottom(head,8);
head=addFromBottom(head,5);
head=addFromBottom(head,2);
head=addFromBottom(head,5);
head=addFromBottom(head,9);
printLinkedList(head);
System.out.println();
Node n=listPartition2(head,5);
printLinkedList(n);
}
}
head=addFromBottom(head,6);
head=addFromBottom(head,5);
head=addFromBottom(head,8);
head=addFromBottom(head,5);
head=addFromBottom(head,5);
head=addFromBottom(head,9);
printLinkedList(head);
System.out.println();
Node n=listPartition2(head,5);
printLinkedList(n);
head=addFromBottom(head,5);
head=addFromBottom(head,5);
head=addFromBottom(head,5);
printLinkedList(head);
System.out.println();
Node n=listPartition2(head,5);
printLinkedList(n);
public static Node copyList(Node head) {
HashMap<Node,Node> map=new HashMap<Node,Node>();
Node p=head;
while (p!=null){
map.put(p,new Node(p.value));
p=p.next;
}
p=head;
while (p!=null){
map.get(p).next=map.get(p.next);
map.get(p).rand=map.get(p.rand);
p=p.next;
}
return map.get(head);
}
4、复制含有随机指针节点的链表 (1)利用HashMap
public static Node copyList(Node head) {
HashMap<Node,Node> map=new HashMap<Node,Node>();
Node p=head;
while (p!=null){
map.put(p,new Node(p.value));
p=p.next;
}
p=head;
while (p!=null){
map.get(p).next=map.get(p.next);
map.get(p).rand=map.get(p.rand);
p=p.next;
}
return map.get(head);
}
(2)单单用链表
package learn;
public class Test {
public static Node copyList2(Node head) {
if(head==null){
return null;
}
Node p=head;
Node next=null;
while (p!=null){
next=p.next;
p.next=new Node(p.value);
p.next.next=next;
p=next;
}
p=head;
while (p!=null){
p.next.rand=(p.rand!=null)?p.rand.next:null;
p=p.next.next;
}
Node copy;
Node headCopy=head.next;
p=head;
while (p!=null){
next=p.next.next;
copy=p.next;
copy.next=next!=null ? next.next:null;
p.next=next;
p=next;
}
return headCopy;
}
public static class Node{
public int value;
public Node next;
public Node rand;
public Node(int value){
this.value=value;
}
}
public static Node add(Node head,int value){
Node cur=new Node(value);
cur.next=head;
head=cur;
return head;
}
public static void printLinkedList(Node head) {
Node n=head;
while (n!=null){
System.out.print(n.value+" ");
n=n.next;
}
}
public static void main(String[] args) {
Node head=null;
head=add(head,3);
head=add(head,2);
head=add(head,1);
Node n2=null;
Node n3=null;
Node p=head;
for(int i=0;i<2;i++){
p=p.next;
if(i==0){
n2=p;
}else if(i==1){
n3=p;
}
}
head.rand=n3;
n2.rand=head;
printLinkedList(head);
System.out.println();
Node h=copyList2(head);
printLinkedList(h);
System.out.println();
System.out.println(h.rand.value);
}
}
|