【数据结构】链表 Linked List

1、leetcode237:删除链表中的节点

请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。

示例 1:
输入: head = [4,5,1,9], node = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.

示例 2:
输入: head = [4,5,1,9], node = 1
输出: [4,5,9]
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# coding:utf-8
class Node:
'''链表节点'''
def __init__(self, x):
self.val = x
self.next = None

def listToLinkedList(list_):
'''将列表转换成链表'''
linkhead = Node(list_[0])
p = linkhead
# 逐个为列表内的数据创建结点, 建立链表
for i in list_[1:]:
p.next = Node(i)
p = p.next
return linkhead # 链表头带着一串尾巴

def printLinkedList1(head):
'''打印链表1'''
if head == None:
return False
print(head.val)
printLinkedList1(head.next)

def printLinkedList2(node):
'''打印链表2(按照字符串打印)'''
if not node:
return "[]"
result = ""
while node:
result += str(node.val) + ", "
node = node.next
print("[" + result[:-2] + "]")

#################################################

l = listToLinkedList([1,2,3,6,3,4,5])

def deleteNode(LinkedList, target):
head = LinkedList
p = head
while p != None:
if p.val == target:
p.val, p.next = p.next.val, p.next.next
else:
p = p.next

deleteNode(l, 3)
printLinkedList2(l)

2、leetcode83:删除排序链表中的重复元素

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

示例 1:
输入: 1->1->2
输出: 1->2

示例 2:
输入: 1->1->2->3->3
输出: 1->2->3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# coding:utf-8
class Node:
'''链表节点'''
def __init__(self, x):
self.val = x
self.next = None

def listToLinkedList(list_):
'''将列表转换成链表'''
linkhead = Node(list_[0])
p = linkhead
# 逐个为列表内的数据创建结点, 建立链表
for i in list_[1:]:
p.next = Node(i)
p = p.next
return linkhead # 链表头带着一串尾巴

def printLinkedList1(head):
'''打印链表1'''
if head == None:
return False
print(head.val)
printLinkedList1(head.next)

def printLinkedList2(node):
'''打印链表2(按照字符串打印)'''
if not node:
return "[]"
result = ""
while node:
result += str(node.val) + ", "
node = node.next
print("[" + result[:-2] + "]")

#################################################

l = listToLinkedList([1,2,2,3])

def deleteDuplicates(head):
if head:
before = head
present = before.next
while present:
if present.val != before.val:
before.next = present
before = present
else:
before.next = None
present = present.next
return head

l1 = deleteDuplicates(l)
printLinkedList2(l1)

3、leetcode206:反转链表

反转一个单链表。

示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Node:
'''链表节点'''
def __init__(self, x):
self.val = x
self.next = None

def listToLinkedList(list_):
'''将列表转换成链表'''
linkhead = Node(list_[0])
p = linkhead
# 逐个为列表内的数据创建结点, 建立链表
for i in list_[1:]:
p.next = Node(i)
p = p.next
return linkhead # 链表头带着一串尾巴

def printLinkedList1(head):
'''打印链表1'''
if head == None:
return False
print(head.val)
printLinkedList1(head.next)

def printLinkedList2(node):
'''打印链表2(按照字符串打印)'''
if not node:
return "[]"
result = ""
while node:
result += str(node.val) + ", "
node = node.next
print("[" + result[:-2] + "]")

#################################################

l = listToLinkedList([1,2,3,4,5])

def reverseList(head):
p, rev = head, None
while p:
rev, rev.next, p = p, rev, p.next
return rev

l1 = reverseList(l)
printLinkedList2(l1)

4、leetcode21:合并两个有序链表

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# coding:utf-8
class Node:
'''链表节点'''
def __init__(self, x):
self.val = x
self.next = None

def listToLinkedList(list_):
'''将列表转换成链表'''
linkhead = Node(list_[0])
p = linkhead
# 逐个为列表内的数据创建结点, 建立链表
for i in list_[1:]:
p.next = Node(i)
p = p.next
return linkhead # 链表头带着一串尾巴

def printLinkedList1(head):
'''打印链表1'''
if head == None:
return False
print(head.val)
printLinkedList1(head.next)

def printLinkedList2(node):
'''打印链表2(按照字符串打印)'''
if not node:
return "[]"
result = ""
while node:
result += str(node.val) + ", "
node = node.next
print("[" + result[:-2] + "]")

#################################################

l1 = listToLinkedList([1,2,3])
l2 = listToLinkedList([3,4,5])

def mergeTwoLists(l1, l2):
if l1 and l2:
if l1.val > l2.val: l1, l2 = l2, l1
l1.next = mergeTwoLists(l1.next, l2)
return l1 or l2

l3 = mergeTwoLists(l1, l2)
printLinkedList2(l3)
0%