LeetCode 所有連結串列題
閒來練手,下面是LeetCode中的所有連結串列問題的答案。
定義單向連結串列的資料結構如下:
public class ListNode {
int val;
ListNode next;
public ListNode(int x) {
val = x;
}
}
難度級別:Easy
83.Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
public ListNode deleteDuplicates(ListNode head) {
ListNode node = head;
while(node != null && node.next != null){
if(node.next.val == node.val){
node.next = node.next.next;
}else {
node = node.next;
}
}
return head;
}
203.Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.
public ListNode removeElements(ListNode head, int val) {
while(head != null && head.val == val){
head = head.next;
}
if (head == null){
return head;
}
ListNode pre = head;
ListNode node = head.next;
while(node != null){
if(node.val == val) {
pre.next = node.next;
}else{
pre = node;
}
node = node.next;
}
return head;
}
234.Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.
public boolean isPalindrome(ListNode head) {
boolean isOdd = true;
if(head == null || head.next == null){
return true;
}
ListNode node1 = head, node2 = head, pre = null, next = node1.next, node3 = null, node4 = null;
while(node2.next != null) {
if(node2.next.next != null) { //node2一定要在node1之前移動
node2 = node2.next.next;
}else{
isOdd = false;
node2 = node2.next;
}
next = node1.next;
node1.next = pre;
pre = node1;
node1 = next;
}
if(isOdd){ //連結串列有奇數個節點
node3 = pre;
node4 = node1.next;
}else{ //連結串列有偶數個節點
node3 = pre;
node4 = node1;
}
while(node3 != null && node4 != null){
if(node3.val != node4.val){
return false;
}
node3 = node3.next;
node4 = node4.next;
}
return true;
}
160.Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode nodeA = headA, nodeB = headB, nodeIntersection = null;
int lengthA = 0, lengthB= 0, lengthDiff=0;
while(nodeA != null) {
lengthA++;
nodeA = nodeA.next;
}
while(nodeB != null) {
lengthB++;
nodeB = nodeB.next;
}
nodeA = headA;
nodeB = headB;
while(lengthA > lengthB){
nodeA = nodeA.next;
lengthA--;
}
while (lengthB > lengthA){
nodeB = nodeB.next;
lengthB--;
}
while(lengthA > 0){
if(nodeA == nodeB){
nodeIntersection = nodeA;
break;
}
lengthA--;
nodeA = nodeA.next;
nodeB = nodeB.next;
}
return nodeIntersection;
}
141.Linked List Cycle
Given a linked list, determine if it has a cycle in it.
public boolean hasCycle(ListNode head) {
if(head == null || head.next == null) {
return false;
}
ListNode node1 = head;
ListNode node2 = head.next.next;
while(node2 != null) {
if(node2 == node1){
return true;
}
node1 = node1.next;
node2 = node2.next;
if(node2 == null){
return false;
}
node2 = node2.next;
}
return false;
}
21.Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode node = head;
while(l1 != null && l2 != null) {
if (l1.val <= l2.val) {
node.next = l1;
l1 = l1.next;
}else {
node.next = l2;
l2 = l2.next;
}
node = node.next;
}
if(l1 == null) {
node.next = l2;
}else {
node.next = l1;
}
return head.next;
}
206.Reverse Linked List
Reverse a singly linked list.
public ListNode reverseList(ListNode head) {
ListNode next = null;
ListNode pre = null;
while(head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
237.Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
public void deleteNode(ListNode node) {
if(node == null || node.next == null) {
return;
}
node.val = node.next.val;
node.next = node.next.next;
}
class MyLinkedList {
private ListNode head;
private int length;
/** Initialize your data structure here. */
public MyLinkedList() {
this.head = new ListNode(0);
this.length = 0;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
public int get(int index) {
if(index < 0 || index >= this.length){
return -1;
}
ListNode node = this.head;
for(int i = 0; i < index; i++){
node = node.next;
}
return node.next.val;
}
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
public void addAtHead(int val) {
ListNode node = this.head.next;
this.head.next = new ListNode(val);
this.head.next.next = node;
this.length++;
}
/** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
ListNode node = this.head;
while(node.next != null){
node = node.next;
}
node.next = new ListNode(val);
this.length++;
}
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
public void addAtIndex(int index, int val) {
if(index < 0 || index > this.length){
return;
}
ListNode node = this.head;
for(int i = 0; i < index; i++){
node = node.next;
}
ListNode next = node.next;
node.next = new ListNode(val);
node.next.next = next;
this.length++;
}
/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if(index < 0 || index >= this.length){
return;
}
ListNode node = this.head;
for(int i = 0; i < index; i++){
node = node.next;
}
node.next = node.next.next;
this.length--;
}
}
上面沒有使用尾部節點,如果使用了,從尾部插入節點該方法的時間複雜度會從O(n)提高到O(1)。
876.Middle of the Linked List
Given a non-empty, singly linked list with head node head, return a middle node of linked list.If there are two middle nodes, return the second middle node.
public ListNode middleNode(ListNode head) {
int length = 0;
ListNode node = head;
while(node != null){
node = node.next;
length++;
}
int half = length / 2;
node = head;
while(half > 0){
half--;
node = node.next;
}
return node;
}