-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list_practice.go
executable file
·93 lines (82 loc) · 1.7 KB
/
linked_list_practice.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import "fmt"
type Node struct{
data int
next *Node
}
func new_node(data int) *Node{
newNode := new(Node)
newNode.data = data
return newNode
}
func RemoveDuplicates(head *Node){
for head.next != nil{
if head.next.data == head.data {
head.next = head.next.next
} else {
head = head.next
}
}
}
func NthNodeFromEnd(head *Node, n int) int{
element, end := head, head
for i:=0; i<n-1; i++{
end = end.next
}
for end.next != nil{
element = element.next
end = end.next
}
return element.data
}
// func CounterClockWise(head *Node, k int){
// temp := head
// for i:=0; i<k; i++{
// temp := temp.next
// }
// end := temp
// for end.next != nil{
// end := end.next
// }
// end.next = head
// temp.next = nil
// }
func SwapElements(head *Node) *Node{
dummy := new_node(-1)
dummy.next = head
ptr1, ptr2, ptr3 := dummy, dummy.next, dummy.next.next
for ptr3 != nil{
ptr1.next = ptr3
ptr2.next = ptr3.next
ptr3.next = ptr2
ptr3, ptr2 = ptr2, ptr3
fmt.Println("after swap")
fmt.Println(ptr1.data,ptr2.data,ptr3.data)
if(ptr3.next != nil && ptr3.next.next != nil){
ptr1 = ptr1.next.next
ptr2 = ptr2.next.next
ptr3 = ptr3.next.next
fmt.Println("after shift")
fmt.Println(ptr1.data,ptr2.data,ptr3.data)
}else{
break
}
}
return dummy
}
func main(){
var node *Node = new_node(10)
var temp *Node = node
for i:=2; i<10; i++{
temp.next = new_node(i*10)
temp = temp.next
}
// RemoveDuplicates(temp)
// fmt.Println(NthNodeFromEnd(node, 3))
dummy := SwapElements(node)
temp = dummy.next
for temp != nil{
fmt.Println(temp.data)
temp = temp.next
}
}