4 鍊錶和遞迴

2021-08-21 03:37:46 字數 4635 閱讀 1092

輸入: 1->2->6->3->4->5->6, val = 6

輸出: 1->2->3->4->5

/**

* definition for singly-linked list.

* public class listnode

* }*/class

solution

if(head == null)

listnode prev = head;

while(prev.next != null)

else

}return head;

}}

public

class

solution2

else

}return dummyhead.next;

}}

# definition for singly-linked list.

# class listnode:

# def __init__(self, x):

# self.val = x

# self.next = none

class

solution:

defremoveelements

(self, head, val):

""" :type head: listnode

:type val: int

:rtype: listnode

"""dummyhead = listnode(-1)

dummyhead.next = head

prev = dummyhead

while prev.next != none:

if prev.next.val == val:

delnode = prev.next

prev.next = delnode.next

delnode.next = none

else:

prev = prev.next

return dummyhead.next

public class listnode 

// 鍊錶節點的建構函式

// 使用arr為引數,

建立乙個鍊錶,

當前的listnode為煉表頭節點

public listnode(int arr)

this.val = arr[0];

listnode cur = this;

for(int i = 1; i < arr.length; i++)

}// 以當前的節點為頭節點餓的鍊錶資訊字串

@override

public string tostring()

return res.tostring();

}

class

solution

// 測試

public

static

void

main

(string args)

; listnode head = new listnode(nums);

system.out.println(head);

listnode res = (new solution()).removeelements(head, 6);

system.out.println(res);

}}

1->2->6->3->4->5->6->null

1->2->3->4->5->null

public

class

sum // 計算arr[l...n)這個區間內所有數字的和

private

static

intsum

(int arr, int l)

return arr[l] + sum(arr, l + 1); // 把原問題轉化為更小的問題

}// 測試

public

static

void

main

(string args)

; system.out.println(sum(nums));

}

36
public

class

solution3

head.next = removeelements(head.next, val);

return head.val == val ? head.next : head;

}// 測試

public

static

void

main

(string args)

; listnode head = new listnode(nums);

system.out.println(head);

listnode res = (new solution3()).removeelements(head, 6);

system.out.println(res);

}}

1->2->6->3->4->5->6->null

1->2->3->4->5->null

public

class

solution3

head.next = removeelements(head.next, val, depth + 1); // depth表示遞迴深度,每次遞迴都+1

system.out.print(depthstring);

system.out.println("after remove" + val + ":" + head.next);

listnode ret;

if (head.val == val) else

system.out.print(depthstring);

system.out.println("return: " + ret);

return ret;

}// 為了列印遞迴深度

private string generatedepthstring

(int depth)

return res.tostring();

}// 測試

public

static

void

main

(string args)

; listnode head = new listnode(nums);

system.out.println(head);

listnode res = (new solution3()).removeelements(head, 6, 0);

system.out.println(res);

}}

1->2->6->3->4->5->6->null

call: remove6 in

1->2->6->3->4->5->6->null

--call: remove6 in

2->6->3->4->5->6->null

----call: remove6 in

6->3->4->5->6->null

------call: remove6 in

3->4->5->6->null

--------call: remove6 in

4->5->6->null

----------call: remove6 in

5->6->null

------------call: remove6 in

6->null

--------------call: remove6 in null

--------------return: null

------------after remove6: null

------------return: null

----------after remove6: null

----------return: 5->null

--------after remove6: 5->null

--------return: 4->5->null

------after remove6: 4->5->null

------return: 3->4->5->null

----after remove6: 3->4->5->null

----return: 3->4->5->null

--after remove6: 3->4->5->null

--return: 2->3->4->5->null

after remove6: 2->3->4->5->null

return: 1->2->3->4->5->null

1->2->3->4->5->null

鍊錶和遞迴

1 leetcode 203 刪除鍊錶元素 不使用虛擬頭結點 public class solution203 if head null return null 中間 listnode prev head while prev.next null else return head 使用虛擬頭結點 對...

反轉鍊錶 遞迴和非遞迴實現

include stdafx.h include include struct node void createlink node head,int data void printlink node head void reverselink node head node reverselink n...

鍊錶專題 4 鍊錶

鍊錶是一大堆節點合起來連起來組成的表的總稱。其中每個節點中都有指標變數指向列表中的下乙個節點。鍊錶中第乙個節點被稱之為表頭 head 所以將第乙個節點的指標變數命名為head。最後乙個節點並沒有神馬特殊的名字,但是它 最後乙個節點 有一項特殊的屬性 最後乙個節點將null作為最後乙個變數的值 所以檢...