1
3
8
4
5
2
| 1 | 4 | 8 | 2 | 5 | 3 |
|---|
| 1 | 4 | 8 | 2 | 5 | 3 |
|---|
1
3
8
4
5
2
| 1 | 4 | 8 | 2 | 5 | 3 |
|---|
1
3
8
4
5
2
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; // Optional
int size; // Optional
}
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_;
}
}public class LinkedList{
private ListNode head;
// the field "head" will always represent the head of the list,
// when the head of the list is changed, we need to assign the new
// memory to the variable "head".
private ListNode tail;
private int size;
public LinkedList() {
head = null;
tail = null;
size = 0;
}
}Java will automatically initialize default values for fields.
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.
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.
public class LinkedList{
private ListNode head = null;
private ListNode tail = null;
private int size = 0;
}Recommended !!!
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;
}public class LinkedList{
public void set(int index, int value) {
checkBoundsExclusive(index);
Listnode node = getEntry(index);
node.val = value;
}
}public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
size++;
ListNode newNode = new ListNode(value);
ListNode pre = getEntry(index-1);
newNode.next = pre.next;
pre.next = newNode;
}
}public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
size++;
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;
}
}public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
size++;
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;
}
}head
public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
size++;
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;
}
}head
newHead
public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
size++;
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;
}
}head
newHead
public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
size++;
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;
}
}head
newHead
public class LinkedList{
public void remove(int index) {
checkBoundsExclusive(index);
size--;
ListNode pre = getEntry(index-1);
pre.next = pre.next.next;
}
}public class LinkedList{
public void remove(int index) {
checkBoundsExclusive(index);
size--;
if (index == 0) {
head = head.next;
return;
}
ListNode pre = getEntry(index-1);
pre.next = pre.next.next;
}
}head
public class LinkedList{
public void remove(int index) {
checkBoundsExclusive(index);
size--;
if (index == 0) {
head = head.next;
return;
}
ListNode pre = getEntry(index-1);
pre.next = pre.next.next;
}
}head
public class LinkedList{
public void remove(int index) {
checkBoundsExclusive(index);
size--;
if (index == 0) {
head = head.next;
return;
}
ListNode pre = getEntry(index-1);
pre.next = pre.next.next;
}
}head
null
没有节点指向头节点!!!
head
null
There is no node pointing at head!!!
head
null
head
fake head
null
head
dummy
public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
size++;
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);
size++;
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);
public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
size++;
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 = head;
index = index - 1;
while (index-- != 0) {
pre = pre.next;
}
public class LinkedList{
public void add(int index, int value) {
checkBoundsExclusive(index);
size++;
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);
size++;
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;
}
}public class LinkedList{
public void remove(int index) {
checkBoundsExclusive(index);
size--;
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;
}
}
/**
* 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) {
}
}public class ListNode {
int val;
ListNode next;
public ListNode(int val) {
this.val = val;
}
}
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
l
l1
l2
2 *(l+l1) = l + l1 + ck
=> l1 + l = ck = (c-1)k + k
=> l = (c-1)k + (k - l1)
=> l = (c-1)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
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
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
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;
}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.
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;
}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;
}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 = 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;
}删除链表中的指定节点. (只知道指定节点的引用).
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;
}This is just a workaround, but doesn't work for the last node.
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;
}node
oriNode
pre
oriNode
instance
oriNode
next
next.next
This is just a workaround, but doesn't work for the last node.
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;
}node
oriNode
pre
oriNode
instance
oriNode
next
next.next
update value
This is just a workaround, but doesn't work for the last node.
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;
}node
oriNode
pre
oriNode
instance
oriNode
null
This is just a workaround, but doesn't work for the last node.
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;
}null
oriNode
pre
oriNode
instance
oriNode
null