連結串列經典示例
連結串列反轉
/**
* 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;
}
相關文章
- BAT 經典演算法筆試題 —— 逆轉單向連結串列BAT演算法筆試
- 【Python】python連結串列應用原始碼示例Python原始碼
- 連結串列 - 單向連結串列
- 連結串列-迴圈連結串列
- 連結串列-雙向連結串列
- 連結串列4: 迴圈連結串列
- 連結串列-雙向通用連結串列
- 連結串列-單連結串列實現
- 連結串列-雙向非通用連結串列
- 【LeetCode】->連結串列->通向連結串列自由之路LeetCode
- 連結串列入門與插入連結串列
- Leetcode_86_分割連結串列_連結串列LeetCode
- 資料結構-單連結串列、雙連結串列資料結構
- 連結串列
- LeetCode-Python-86. 分隔連結串列(連結串列)LeetCodePython
- 單連結串列建立連結串列出現問題
- **203.移除連結串列元素****707.設計連結串列****206.反轉連結串列**
- php連結串列PHP
- 連結串列逆序
- 2、連結串列
- 連結串列(python)Python
- 重排連結串列
- 單連結串列
- 分割連結串列
- (一)連結串列
- 資料結構與演算法——連結串列 Linked List(單連結串列、雙向連結串列、單向環形連結串列-Josephu 問題)資料結構演算法
- 【LeetCode-連結串列】面試題-反轉連結串列LeetCode面試題
- 資料結構之連結串列:206. 反轉連結串列資料結構
- 反轉連結串列、合併連結串列、樹的子結構
- 既然已經有陣列了,為什麼還要連結串列?JS連結串列(Linked-list)詳解陣列JS
- [連結串列】2.輸入一個連結串列,反轉連結串列後,輸出新連結串列的表頭。[多益,位元組考過]
- 程式碼隨想錄第3天 | 連結串列 203.移除連結串列元素,707.設計連結串列,206.反轉連結串列
- 【圖解連結串列類面試題】移除連結串列元素圖解面試題
- 【圖解連結串列類面試題】環形連結串列圖解面試題
- 資料結構實驗之連結串列二:逆序建立連結串列資料結構
- 資料結構--陣列、單向連結串列、雙向連結串列資料結構陣列
- 資料結構實驗之連結串列九:雙向連結串列資料結構
- Linux核心連結串列Linux