連結串列經典示例
連結串列反轉
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> arr = new ArrayList<>();
if(listNode == null){
return arr;
}
ListNode pre = listNode;
ListNode rear = pre.next;
pre.next = null;
while(rear != null){
ListNode tmp = rear.next;
rear.next = pre;
pre = rear;
rear = tmp;
}
while(pre != null){
arr.add(pre.val);
pre = pre.next;
}
return arr;
}
}
倒數第k個節點
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head == null || k == 0){
return null;
}
ListNode first = head;
ListNode second = head;
for(int i = 0; i < k - 1 && second != null; ++i){
second = second.next;
}
if(second == null){
return null;
}
while(second.next != null){
first = first.next;
second = second.next;
}
return first;
}
}
連結串列合併
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
if(list1 == null){
return list2;
}
if(list2 == null){
return list1;
}
ListNode l1 = list1;
ListNode l2 = list2;
ListNode head = null;
ListNode last = null;
while(l1 != null && l2 != null){
if(l1.val <= l2.val){
if(last == null){
head = l1;
last = l1;
}else{
last.next = l1;
last = l1;
}
l1 = l1.next;
}else{
if(last == null){
head = l2;
last = l2;
}else{
last.next = l2;
last = l2;
}
l2 = l2.next;
}
}
if(l1 != null){
last.next = l1;
}
if(l2 != null){
last.next = l2;
}
return head;
}
}
連結串列複製
public static RandomListNode Clone(RandomListNode pHead) {
if (pHead == null) {
return null;
}
RandomListNode first = pHead;
while (first != null) {
RandomListNode tmp = first.next;
RandomListNode newNode = new RandomListNode(first.label);
first.next = newNode;
newNode.next = tmp;
first = tmp;
}
RandomListNode two = pHead;
while (two != null) {
if(two.random != null){
two.next.random = two.random.next;
}
two = two.next.next;
}
RandomListNode cur = pHead;
RandomListNode result = pHead.next;
while(cur.next != null){
RandomListNode tmp = cur.next;
cur.next = tmp.next;
cur = tmp;
}
return result;
}
兩個連結串列的公共節點
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
if(pHead1 == null || pHead2 == null){
return null;
}
ListNode cur1 = pHead1;
ListNode cur2 = pHead2;
int length1 = 0;
int length2 = 0;
while(cur1 != null){
length1++;
cur1 = cur1.next;
}
while(cur2 != null){
length2++;
cur2 = cur2.next;
}
cur1 = pHead1;
cur2 = pHead2;
if(length2 >= length1){
for(int i = 0; i < length2-length1; ++i){
cur2 = cur2.next;
}
}else{
for(int i = 0; i < length1-length2; ++i){
cur1 = cur1.next;
}
}
while(cur1 != null){
if(cur1 == cur2){
return cur1;
}
cur1 = cur1.next;
cur2 = cur2.next;
}
return null;
}
判斷連結串列有環
public ListNode EntryNodeOfLoop(ListNode pHead)
{
if(pHead == null){
return null;
}
ListNode fast = pHead;
ListNode slow = pHead;
do{
fast = fast.next;
if(fast == null){
return null;
}
fast = fast.next;
slow = slow.next;
}while(fast != slow);
fast = pHead;
while(fast != slow){
fast = fast.next;
slow = slow.next;
}
return slow;
}
刪除連結串列重複節點
public ListNode deleteDuplication(ListNode pHead)
{
if(pHead == null){
return null;
}
ListNode head = new ListNode(-1);
head.next = pHead;
ListNode pre = head;
ListNode next1 = pHead;
ListNode next2 = pHead.next;
while(next2 != null){
if(next2.val == next1.val){
while(next2 != null && next2.val == next1.val){
next2 = next2.next;
next1 = next1.next;
}
if(next2 == null){
pre.next = next2;
break;
}else{
pre.next = next2;
next1 = next2;
next2 = next2.next;
}
}else{
pre = next1;
next1 = next2;
next2 = next2.next;
}
}
return head.next;
}
相關文章
- C/C++筆試經典——連結串列倒序C++筆試
- BAT 經典演算法筆試題 —— 逆轉單向連結串列BAT演算法筆試
- VC++基礎 連結串列的操作示例C++
- 連結串列-雙向連結串列
- 連結串列-迴圈連結串列
- 連結串列面試題(二)---連結串列逆序(連結串列反轉)面試題
- 連結串列4: 迴圈連結串列
- 連結串列-單連結串列實現
- 【Python】python連結串列應用原始碼示例Python原始碼
- 連結串列入門與插入連結串列
- (連結串列)連結串列的排序問題排序
- 連結串列
- 寒假專案1-動態連結串列體驗(示例)
- 經典演算法題每日演練——第二十五題 塊狀連結串列演算法
- 【Java】經典示例程式碼Java
- javascript中的連結串列結構—雙向連結串列JavaScript
- Leetcode_86_分割連結串列_連結串列LeetCode
- 【LeetCode】->連結串列->通向連結串列自由之路LeetCode
- php連結串列PHP
- 連結串列操作
- 連結串列逆序
- 2、連結串列
- 【圖解連結串列類面試題】移除連結串列元素圖解面試題
- 【圖解連結串列類面試題】環形連結串列圖解面試題
- 連結串列以及golang介入式連結串列的實現Golang
- Linux核心連結串列-通用連結串列的實現Linux
- 資料結構-單連結串列、雙連結串列資料結構
- 反轉連結串列、合併連結串列、樹的子結構
- 資料結構與演算法——連結串列 Linked List(單連結串列、雙向連結串列、單向環形連結串列-Josephu 問題)資料結構演算法
- Java基礎系列22:有關連結串列的經典面試題目解析與程式碼實現Java面試題
- 【LeetCode-連結串列】面試題-反轉連結串列LeetCode面試題
- 連結串列專題——面試中常見的連結串列問題面試
- LeetCode-Python-86. 分隔連結串列(連結串列)LeetCodePython
- 連結串列面試題(七)---合併兩個有序連結串列面試題
- 既然已經有陣列了,為什麼還要連結串列?JS連結串列(Linked-list)詳解陣列JS
- 資料結構實驗之連結串列九:雙向連結串列資料結構
- 資料結構實驗之連結串列二:逆序建立連結串列資料結構
- 資料結構--陣列、單向連結串列、雙向連結串列資料結構陣列