- Linked List Cycle
Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a parameter.
Return true if there is a cycle in the linked list. Otherwise, return false.
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution1 {
public boolean hasCycle(ListNode head) {
if(head == null){
return false;
}
ListNode fast=head;
ListNode slow=head;
while(fast !=null && fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow){return true;}
}
return false;
}
}
//'Floyd's Tortoise and Hare', official answer
public class Solution2 {
public boolean hasCycle(ListNode head) {
if(head == null){
return false;
}
ListNode fast=head.next;
ListNode slow=head;
while(fast!=slow){
if(fast==null || fast.next==null){return false;}
fast=fast.next.next;
slow=slow.next;
}
return true;
}
}
- Delete Node in a Linked List
Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly.
It is guaranteed that the node to be deleted is not a tail node in the list.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
if(node == null){return;}
node.val=node.next.val;
node.next=node.next.next;
}
}
- Remove Duplicates from Sorted List:
Given a sorted linked list, delete all duplicates such that each element appear only once.
/**
* 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 Solution1 {
public ListNode deleteDuplicates(ListNode head) {
ListNode current = head;
while(current!=null && current.next!=null){
if(current.val==current.next.val){
current.next=current.next.next;
}else{
current=current.next;
}
}
return head;
}
}
//faster solution
class Solution2 {
public ListNode deleteDuplicates(ListNode head) {
if (head == null){
return null;
}
ListNode prev = head;
ListNode current = head.next;
while(current!=null){
if(prev.val==current.val){
prev.next=current.next;
}else{
prev=current;
}
current=current.next;
}
return head;
}
}
- Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
begin to intersect at node c1.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null || headB==null){
return null;
}
ListNode a = headA;
ListNode b = headB;
while(a!=b){
a = a==null? headB : a.next;
b = b==null? headA : b.next;
}
return a;
}
}
- Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.
/**
* 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 removeElements(ListNode head, int val) {
if(head==null){
return null;
}
ListNode p = head;
while(p!=null && p.val==val){
p=p.next;
head=p;
}
while(p!=null){
if(p.next!=null && p.next.val==val){
p.next=p.next.next;
}else{
p=p.next;
}
}
return head;
}
}
//similar speed, but it's pretty neat code to use fakehead
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode fakehead = new ListNode(-1);
fakehead.next = head;
ListNode current=head, prev=fakehead;
while(current!=null){
if(current.val==val){
prev.next=current.next;
}else{
prev=prev.next;
}
current=current.next;
}
return fakehead.next;
}
}
- Reverse Linked List
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
/**
* 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 || head.next==null){
return head;
}
ListNode a=head;
ListNode b=head.next;
a.next=null;
while(b!=null){
ListNode tmp=b.next;
b.next=a;
a=b;
b=tmp;
}
return a;
}
}
- Merge Two Sorted Lists
Input: l1 = [1,2,4], l2 = [1,3,4]
Output: [1,1,2,3,4,4]
/**
* 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 mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null) return l2;
if(l2==null) return l1;
ListNode result,p;
if(l1.val<l2.val){
result = l1;
l1=l1.next;
}else{
result=l2;
l2=l2.next;
}
p=result;
while(l1!=null&&l2!=null){
if(l1.val<l2.val){
p.next=l1;
l1=l1.next;
}else{
p.next=l2;
l2=l2.next;
}
p=p.next;
}
if(l1!=null){
p.next=l1;
}else{
p.next=l2;
}
return result;
}
}
//思路类似,写法更简洁,注意一开始新建的那个node不在结果里
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode result=new ListNode();
ListNode p=new ListNode();
p=result;
while(l1!=null && l2!=null){
if(l1.val<l2.val){
p.next=new ListNode(l1.val);
l1=l1.next;
}else{
p.next=new ListNode(l2.val);
l2=l2.next;
}
p=p.next;
}
if(l1!=null){
p.next=l1;
}else{
p.next=l2;
}
return result.next;
}
}
- Palindrome Linked List
/**
* 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 boolean isPalindrome(ListNode head) {
ListNode leftList = head;
ListNode midNode = head;
int len=calculateListLength(head);
int mid= len%2==0? len/2 : len/2+1;
while(mid>0){
midNode=midNode.next;
mid-=1;
}
ListNode rightList=reverseList(midNode);
int compareCount=len/2;
while(compareCount>0){
if(leftList.val != rightList.val){
return false;
}
leftList=leftList.next;
rightList=rightList.next;
compareCount-=1;
}
return true;
}
private int calculateListLength(ListNode head){
int count=0;
while(head!=null){
count+=1;
head=head.next;
}
return count;
}
private ListNode reverseList(ListNode head){
ListNode a=null;
ListNode b=head;
while(b!=null){
ListNode tmp=b.next;
b.next=a;
a=b;
b=tmp;
}
return a;
}
}