Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
5
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
5
a
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
obj
?
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
MyObject instance
obj
?
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
MyObject instance
obj
addr1
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
MyObject instance
addr1
obj
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
MyObject instance
addr1
obj
1 |
---|
1 |
2016 |
Date instance
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
MyObject instance
addr1
obj
1 |
---|
1 |
2016 |
addr2
Date instance
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
MyObject instance
addr1
obj
1 |
---|
1 |
2016 |
addr2
date
Date instance
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
MyObject instance
addr1
obj1
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
MyObject instance
addr1
obj1
obj2
addr1
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Memory
Heap Memory
Stack Memory
MyObject instance
addr1
obj1
obj2
addr2
MyObject instance
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
String a = "abc";
String b = "abc";
System.out.println(a == b); // true
String c = new String("abc");
System.out.println(a == c); // false
System.out.println(a.equals(c)); // trye
String d = new String("abc");
System.out.println(c == d); // false
System.out.println(d.equals(c)); // true
Integer num1 = 1;
Integer num2 = 1;
System.out.println(num1 == num2); // true
Integer num3 = new Integer(1);
System.out.println(num1 == num3); // false
Integer num4 = new Integer(1);
System.out.println(num3 == num4); // false
System.out.println(num3.equals(num4)); // true
// OPTIONAL OPTIONAL******
Integer num5 = new Integer(0);
System.out.println(num3 == num4+num5); // true
Use equals function for reference comparison
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public void increase(int a) {
a = a + 1;
System.out.print(a);
}
public static void main() {
int a = 5;
increase(a);
System.out.print(a);
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public void increase(int a) {
a = a + 1;
System.out.print(a);
}
public static void main() {
int a = 5;
increase(a); // print 6.
System.out.print(a); // print 5
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public void increase(MyObject obj) {
obj.a += 1;
System.out.print(obj.a);
}
public static void main() {
MyObject obj = new MyObject();
obj.a = 5;
increase(obj);
System.out.print(obj.a);
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public void increase(MyObject obj) {
obj.a += 1;
System.out.print(obj.a);
}
public static void main() {
MyObject obj = new MyObject();
obj.a = 5;
increase(obj); // print 6.
System.out.print(obj.a); // print 6
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public void increase(MyObject obj) {
MyObject obj2 = obj;
obj2.a += 1;
System.out.print(obj.a);
System.out.print(obj2.a);
}
public static void main() {
MyObject obj = new MyObject();
obj.a = 5;
increase(obj);
System.out.print(obj.a);
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public void change(String str) {
str = new String("world");
System.out.print(str);
}
public static void main() {
String s = new String("hello");
change(s); // print 'world';
System.out.print(s); // print 'hello'.
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public void increase(MyObject obj) {
MyObject obj2 = obj;
obj2.a += 1;
System.out.print(obj.a);
System.out.print(obj2.a);
}
public static void main() {
MyObject obj = new MyObject();
obj.a = 5;
increase(obj); // print 6, then print 6.
System.out.print(obj.a);
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public void increase(MyObject obj) {
MyObject obj2 = obj;
obj2.a += 1;
System.out.print(obj.a);
System.out.print(obj2.a);
}
public static void main() {
MyObject obj = new MyObject();
obj.a = 5;
increase(obj); // print 6, then print 6.
System.out.print(obj.a); // print 6.
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Address of the real object.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
1
3
8
4
5
2
Copyright © 直通硅谷
http://www.zhitongguigu.com/
1 | 4 | 8 | 2 | 5 | 3 |
---|
Copyright © 直通硅谷
http://www.zhitongguigu.com/
1 | 4 | 8 | 2 | 5 | 3 |
---|
1
3
8
4
5
2
Copyright © 直通硅谷
http://www.zhitongguigu.com/
1 | 4 | 8 | 2 | 5 | 3 |
---|
1
3
8
4
5
2
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class ListNode {
int val;
ListNode next;
ListNode(int val_) {
val = val_;
next = null; // this line is optional
}
}
public class LinkedList {
ListNode head;
ListNode tail;
int size;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
// TODO: implement this class.
public int get(int index);
public void set(int index, int value);
public void add(int index, int value);
public void remove(int index);
public void removeByValue(int value);
}
public class ListNode {
int val;
ListNode next;
public ListNode(int val_) {
this.val = val_;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
private ListNode head;
private ListNode tail;
private int size;
public LinkedList() {
head = null;
tail = null;
size = 0;
}
}
Java will automatically initialize the value for the fields.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
private ListNode head;
private ListNode tail;
private int size;
public LinkedList() {
}
}
Java will automatically generate a non-param constructor if you don't have other constructors.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
private ListNode head;
private ListNode tail;
private int size;
}
Java will automatically generate a non-param constructor if you don't have other constructors.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
private ListNode head = null;
private ListNode tail = null;
private int size = 0;
}
Recommend !!!
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
public void checkBoundsExclusive(int index) {
if (index < 0 || index >= size) {
// throw Exception.
}
}
public ListNode getEntry(int index) {
ListNode cur = head;
for (int i = 0; i < index; i++) {
cur = cur.next;
}
return cur;
}
public int get(int index) {
checkBoundsExclusive(index);
return getEntry(index).val;
}
}
public ListNode getEntry(int index) {
ListNode cur = head;
while (index-- != 0) {
cur = cur.next;
}
return cur;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
public void set(int index, int value) {
checkBoundsExclusive(index);
Listnode node = getEntry(index);
node.val = value;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
ListNode newNode = new ListNode(value);
ListNode pre = getEntry(index-1);
newNode.next = pre.next;
pre.next = newNode;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
ListNode newNode = new ListNode(value);
if (index == 0) {
newNode.next = head;
head = newNode;
return;
}
ListNode pre = getEntry(index-1);
newNode.next = pre.next;
pre.next = newNode;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
public void remove(int index) {
checkBoundsExclusive(index);
ListNode pre = getEntry(index-1);
pre.next = pre.next.next;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
public void remove(int index) {
checkBoundsExclusive(index);
if (index == 0) {
head = head.next;
return;
}
ListNode pre = getEntry(index-1);
pre.next = pre.next.next;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
null
There is no node pointing at head!!!
head
Copyright © 直通硅谷
http://www.zhitongguigu.com/
null
There is no node pointing at head!!!
head
Copyright © 直通硅谷
http://www.zhitongguigu.com/
null
head
fake head
Copyright © 直通硅谷
http://www.zhitongguigu.com/
null
head
dummy
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
while (index-- != 0) {
pre = pre.next;
}
ListNode newNode = new ListNode(value);
newNode.next = pre.next;
pre.next = newNode;
head = dummy.next;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
while (index-- != 0) {
pre = pre.next;
}
ListNode newNode = new ListNode(value);
newNode.next = pre.next;
pre.next = newNode;
head = dummy.next;
}
}
ListNode pre = getEntry(index-1);
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
while (index--) {
pre = pre.next;
}
ListNode newNode = new ListNode(value);
newNode.next = pre.next;
pre.next = newNode;
head = dummy.next;
}
}
ListNode pre = head;
index = index - 1;
while (index-- != 0) {
pre = pre.next;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
while (index-- != 0) {
pre = pre.next;
}
ListNode newNode = new ListNode(value);
newNode.next = pre.next;
pre.next = newNode;
head = dummy.next;
}
}
public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
ListNode newNode = new ListNode(value);
if (index == 0) {
newNode.next = head;
head = newNode;
return;
}
ListNode pre = head;
index = index - 1;
while (index-- != 0) {
pre = pre.next;
}
newNode.next = pre.next;
pre.next = newNode;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class LinkedList{
public void remove(int index) {
checkBoundsExclusive(index);
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
while (index-- != 0) {
pre = pre.next;
}
pre.next = pre.next.next;
head = dummy.next;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public class ListNode {
int val;
ListNode next;
public ListNode(int val) {
this.val = val;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public int length(ListNode head) {
ListNode cur = head;
int length = 0;
while (cur != null) {
length++;
cur = cur.next;
}
return length;
}
Given a linked list, return the length of it.
Examples:
Input: 1->4->2->3
Output: 4
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given a linked list, return the kth node from the end. Linked list will never be empty and k will always be valid.
Examples:
Input: 1->4->2->3, 2
Output: 2
Input: 3->5->9->6->8, 3
Output: 9
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public ListNode kthNodeFromEnd(
ListNode head, int k) {
int length = length(head);
int index = length - k;
ListNode cur = head;
int count = 0;
while (count < index) {
cur = cur.next;
count++;
}
return cur;
}
public ListNode kthNodeFromEnd(
ListNode head, int k) {
int length = length(head);
int index = length - k;
ListNode cur = head;
while (index-- != 0) {
cur = cur.next;
}
return cur;
}
Given a linked list, return the kth node from the end. Linked list will never be empty and k will always be valid.
Examples:
Input: 1->4->2->3, 2
Output: 2
Input: 3->5->9->6->8, 3
Output: 9
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public ListNode kthNodeFromEnd(
ListNode head, int k) {
ListNode first = head;
while (k-- != 0) {
first = first.next;
}
ListNode second = head;
while (first != null) {
first = first.next;
second = second.next;
}
return second;
}
Given a linked list, return the kth node from the end. Linked list will never be empty and k will always be valid.
Examples:
Input: 1->4->2->3, 2
Output: 2
Input: 3->5->9->6->8, 3
Output: 9
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public ListNode midNode(ListNode head) {
int length = length(head);
int index = (length - 1) / 2;
ListNode cur = head;
while (index-- != 0) {
cur = cur.next;
}
return cur;
}
Given a linked list, return the middle node. Linked list will never be empty.
Examples:
Input: 1->4->2->3
Output: 4
Input: 3->5->9->6->8
Output: 9
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public ListNode midNode(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast.next != null &&
fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
Given a linked list, return the middle node. Linked list will never be empty.
Examples:
Input: 1->4->2->3
Output: 4
Input: 3->5->9->6->8
Output: 9
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public boolean hasCycle(ListNode head) {
if (head == null) {
return false;
}
ListNode fast = head;
ListNode slow = head;
while (fast != null) {
if (fast.next == null) {
return false;
}
if (fast.next == slow) {
return true;
}
fast = fast.next.next;
slow = slow.next;
}
return false;
}
Given a linked list, define if there is a cycle in it.
Examples:
Input: 1->4->2->3
Output: false.
Input: 3->5->9->3(original)
Output: true
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given a linked list, return the node where the cycle begins or null.
Examples:
Input: 1->4->2->3
Output: null.
Input: 3->5->9->3(original)
Output: 3
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given a linked list, return the node where the cycle begins or null.
Examples:
Input: 1->4->2->3
Output: null.
Input: 3->5->9->3(original)
Output: 3
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given a linked list, return the node where the cycle begins or null.
Examples:
Input: 1->4->2->3
Output: null.
Input: 3->5->9->3(original)
Output: 3
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given a linked list, return the node where the cycle begins or null.
Examples:
Input: 1->4->2->3
Output: null.
Input: 3->5->9->3(original)
Output: 3
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given a linked list, return the node where the cycle begins or null.
Examples:
Input: 1->4->2->3
Output: null.
Input: 3->5->9->3(original)
Output: 3
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given a linked list, return the node where the cycle begins or null.
Examples:
Input: 1->4->2->3
Output: null.
Input: 3->5->9->3(original)
Output: 3
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given a linked list, return the node where the cycle begins or null.
Examples:
Input: 1->4->2->3
Output: null.
Input: 3->5->9->3(original)
Output: 3
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given a linked list, return the node where the cycle begins or null.
Examples:
Input: 1->4->2->3
Output: null.
Input: 3->5->9->3(original)
Output: 3
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given a linked list, return the node where the cycle begins or null.
Examples:
Input: 1->4->2->3
Output: null.
Input: 3->5->9->3(original)
Output: 3
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given a linked list, return the node where the cycle begins or null.
Examples:
Input: 1->4->2->3
Output: null.
Input: 3->5->9->3(original)
Output: 3
Copyright © 直通硅谷
http://www.zhitongguigu.com/
k
l1
l2
2 *(k+l1) = k + l1 + l2 + l1
=> k = l2
public ListNode detectCycle(ListNode head) {
ListNode fast = head, slow = head;
while (fast != null && slow != null) {
if (fast.next != null) {
fast = fast.next.next;
} else {
return null;
}
slow = slow.next;
if (fast == slow) {
ListNode temp = head;
while (temp != slow) {
temp = temp.next;
slow = slow.next;
}
return slow;
}
}
return null;
}
Given a linked list, return the node where the cycle begins or null.
Examples:
Input: 1->4->2->3
Output: null.
Input: 3->5->9->3(original)
Output: 3
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public ListNode removeDuplicates(
ListNode head) {
if (head == null) {
return null;
}
ListNode pre = head;
while (pre.next != null) {
if (pre.val == pre.next.val) {
pre.next = pre.next.next;
} else {
pre = pre.next;
}
}
return head;
}
Given a sorted linked list, remove all duplicates such that each element appear only once.
Examples:
Input: 1->2->2->2->3->3
Output: 1->2->3
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public ListNode removeDuplicates(ListNode head) {
if (head == null) {
return null;
}
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
while (pre.next != null && pre.next.next != null) {
if (pre.next.val == pre.next.next.val) {
int lastVal = pre.next.val;
while (pre.next != null && pre.next.val == lastVal) {
pre.next = pre.next.next;
}
} else {
pre = pre.next;
}
}
return dummy.next;
}
Given a sorted linked list, remove all nodes that have duplicate numbers leaving only the distinct numbers from the original list.
Examples:
Input: 1->2->2->2->3->3
Output: 1
Input: 3->4->4->5->6
Output: 3->5->6
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Merge two sorted list and return it as a new list.
Examples:
Input: 1->2->2->2, -1->3->4->5
Output: -1->1->2->2->2->3->4->5
public ListNode merge(ListNode head1, ListNode head2) {
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
while (head1 != null && head2 != null) {
if (head1.val < head2.val) {
cur.next = new ListNode(head1.val);
head1 = head1.next;
} else {
cur.next = new ListNode(head2.val);
head2 = head2.next;
}
cur = cur.next;
}
while (head2 != null) {
cur.next = new ListNode(head2.val);
head2 = head2.next;
cur = cur.next;
}
while (head1 != null) {
cur.next = new ListNode(head1.val);
head1 = head1.next;
cur = cur.next;
}
return dummy.next;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Merge two sorted list and return it as a new list.
Examples:
Input: 1->2->2->2, -1->3->4->5
Output: -1->1->2->2->2->3->4->5
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public ListNode merge(ListNode head1, ListNode head2) {
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
while (head1 != null && head2 != null) {
if (head1.val < head2.val) {
cur.next = head1;
head1 = head1.next;
} else {
cur.next = head2;
head2 = head2.next;
}
cur = cur.next;
}
if (head1 == null) {
cur.next = head2;
} else if (head2 == null) {
cur.next = head1;
}
return dummy.next;
}
Reverse a given linked list.
Examples:
Input: 1->2->3->4
Output: 4->3->2->1
public ListNode reverse(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while (cur != null) {
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given a linked list, swap every two adjacent nodes and return its head.
Examples:
Input: 1->2->3->4
Output: 2->1->4->3
Note: Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given a linked list, swap every two adjacent nodes and return its head.
Examples:
Input: 1->2->3->4
Output: 2->1->4->3
Note: Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
while (pre.next != null && pre.next.next != null) {
ListNode first = pre.next, second = pre.next.next;
first.next = second.next;
second.next = first;
pre.next = second;
pre = first;
}
return dummy.next;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
while (pre.next != null && pre.next.next != null) {
if (pre.next.val == pre.next.next.val) {
int lastVal = pre.next.val;
while (pre.next != null &&
pre.next.val == lastVal) {
pre.next = pre.next.next;
}
} else {
pre = pre.next;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given one node in a linked list, remove that node. (Only the reference to the specific node is given).
Examples:
Input: 1->2->3->4, 3
Output: 1->2->4
public void removeNode(ListNode node) {
if (node == null) {
return;
}
if (node.next == null) {
node = null;
return;
}
node.val = node.next.val;
node.next = node.next.next;
return;
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Given two linked list (without circle), detect if they intersect with each other.
Implement:
Java: public boolean detectIntersection(ListNode head1, ListNode head2);
c++: bool detectIntersection(ListNode* head1, ListNode* head2);
Follow up: Return null or the intersection ListNode.
Java: public ListNode detectIntersection(ListNode head1, ListNode head2);
c++: ListNode* detectIntersection(ListNode* head1, ListNode* head2);
Follow up 2: What if the linked lists may have circle? Return null or the intersection node.
Copyright © 直通硅谷
http://www.zhitongguigu.com/